资源描述:
《javawebsshjxl导出excel实例》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、JavaWebSSHJXL导出ExcelJsp:functionaaa(){//提交方式一定不能为ajax,否则不会出现保存对话框varfrm=document.getElementById("searchwmsgform");frm.action="<%=web%>/select.do?dispatch=exportExcel";frm.submit();}Ac
2、tion:/***导出Excel*@parammapping*@paramform*@paramrequest*@paramresponse*@return*/publicActionForwardexportExcel(ActionMappingmapping,ActionFormform,HttpServletRequestrequest,HttpServletResponseresponse){try{StringfilePath=request.getRealPath("/");Filefile=newFile(filePath+"报警信息.xls"
3、);WritableWorkbookwwb=Workbook.createWorkbook(file);//生成ExcelselectService.exportExcel(wwb);try{//创建一个缓冲输入流对象BufferedInputStreambr=newBufferedInputStream(newFileInputStream(file));byte[]buf=newbyte[1024];intlen=0;response.reset();//非常重要response.setContentType("application/x-msdownl
4、oad");response.setHeader("Content-Disposition","attachment;filename="+newString(file.getName().getBytes(),"iso-8859-1"));//创建输出流对象OutputStreamoutStream=response.getOutputStream();//开始输出while((len=br.read(buf))>0)outStream.write(buf,0,len);//关闭流对象br.close();outStream.close();}catch(
5、FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}if(file.exists()){//下载完毕删除文件file.delete();}}catch(Exceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}returnnull;}实现类中:publicvoidexportExcel(WritableWorkbookbook)throwsException{try{Wri
6、tableSheetsheet=book.createSheet("第一页",0);//字段字体jxl.write.WritableFontwfc1=newjxl.write.WritableFont(WritableFont.ARIAL,13,WritableFont.BOLD,false);jxl.write.WritableCellFormatwcfFC1=newjxl.write.WritableCellFormat(wfc1);//水平居中wcfFC1.setAlignment(jxl.format.Alignment.CENTRE);//垂直居中
7、wcfFC1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//边框wcfFC1.setBorder(Border.ALL,BorderLineStyle.THIN);Stringtitle="序号,时间,源IP地址,目标IP地址,源MAC地址,目标MAC地址,错误信息";String[]splitsTitle=title.split(",");intlength=splitsTitle.length;for(inti=0;i8、l.write.Label(i,0,splitsTi