java导出Excel文件以及文件的下载、剔除
java导出Excel文件以及文件的下载、删除内容包括,内容导出为excel格式,以及生成以后此excel的下载和删除。
java导出Excel文件以及文件的下载、删除
内容包括,内容导出为excel格式,以及生成以后此excel的下载和删除。
首先定义方法,将要导出的数据通过参数传输过来:

1、创建Excel文件:
?
[java] view plaincopy
- String?fileName?=?"C:\\统计数据"+startDate2+"-"+endDate2+".xls";???WritableWorkbook?workbook?=Workbook.createWorkbook(new?File(fileName));??
2、创建数据样式:
[java] view plaincopy
- //字体:??//字体大小,字体类型。??
- WritableFont??wr?=?newWritableFont(WritableFont.TIMES,14,WritableFont.BOLD);??//创建字体样式引用??
- WritableCellFormat?fontFormat1?=?new?WritableCellFormat(wr);??//对其方式??
- fontFormat1.setAlignment(jxl.format.Alignment.CENTRE);??//是否有边框??
- fontFormat1.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);???????????????
- //时间:??jxl.write.DateFormat?df?=?newjxl.write.DateFormat("yyyy-MM-dd");??
- WritableCellFormat?dateFormat?=?new?WritableCellFormat(df);??dateFormat.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);??
- dateFormat.setAlignment(jxl.format.Alignment.CENTRE);????
- //数字:??jxl.write.NumberFormat?nf?=?newjxl.write.NumberFormat("###,###,###,###,###");??
- WritableCellFormat?numberFormat?=?new?WritableCellFormat(nf);??numberFormat.setAlignment(jxl.format.Alignment.CENTRE);??
- numberFormat.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);???//建立第N个sheet??
- WritableSheet?sheet?=?workbook.createSheet("名称",N);?????//sheet的第几列,和列宽。??
- sheet.setColumnView(0,6);??sheet.setColumnView(1,14);??
- sheet.setColumnView(2,12);?????//合并单元格(起始列,起始行,结束列,结束行)??
- sheet.mergeCells(1,0,2,0);????//向sheet中写入数据(列,行,内容,格式)??
- Label?label0?=?new?Label(1,0,"1992-04-06",dateFormat);?????????????sheet.addCell(label0);??
- Label?label2?=?new?Label(1,1,"许阳",fontFormat2);?????????????sheet.addCell(label2);??
- Label?label3?=?new?Label(2,1,"21",numberFormat1);?????????????sheet.addCell(label3);??
- workbook.write();??workbook.close();??
以上excel写入数据完成,并保存到了指定的位置。若是在本地操作,则没问题,若是在服务器上,就得使用java的输入输出流写入到本地(下载)。
调用downLoadFile方法。将文件位置用参数传过去。
[java] view plaincopy
- this.downLoadFile(fileName);??
this.downLoadFile(fileName);
downLoadFile方法:
[java] view plaincopy
- public?void?downLoadFile(String?filePth)?{?????????HttpServletResponse?response?=ServletActionContext.getResponse();??
- ???????HttpServletRequest?request?=ServletActionContext.getRequest();?????????try?{??
- ?????????????//得到当前路径??
- ???????????//StringfilePath=request.getSession().getServletContext().getRealPath(File.separator);?????????????File?temFile?=?new?File(filePth);??
- ??????????//判断文件是否存在?????????????if(!temFile.exists()){??
- ???????????????response.getWriter().write("ERROR:File?Not?Found");?????????????????return?;??
- ???????????}????????????//处理文件名得位置(若服务器为linux和windows的处理方法不同)??
- ???????????String?fileName?=filePth.substring(filePth.lastIndexOf(File.separator)+1);???????????//设置头文件,名称和内容的编码不同,否则会出现乱码。??
- ???????????response.setHeader("Content-Disposition",?"attachment;?filename="+new?String((fileName).getBytes("gbk"),"iso8859-1"));????
- ???????????response.setContentType("application/x-download");?????????????OutputStream?ot=response.getOutputStream();??
- ???????????BufferedInputStream?bis??=?newBufferedInputStream(new?FileInputStream(temFile));?????????????BufferedOutputStream?bos?=?new?BufferedOutputStream(ot);??
- ???????????byte[]?buffer?=?new?byte[4096];?????????????int?length?=?0;??
- ???????????while((length?=?bis.read(buffer))?>?0){?????????????????bos.write(buffer,0,length);??
- ???????????}?????????????bos.close();??
- ???????????bis.close();?????????????ot.close();??
- ???????}?catch?(Exception?e)?{?????????????e.printStackTrace();??
- ???????}??????}??
此时就会下载完成,完成后需要在服务器删除此Excel文件:
在this.downLoadFile方法后面继续调用删除的方法:
[java] view plaincopy
- public?boolean?deleteFile(String?sPath)?{?????boolean?flag?=?false;??
- ??//处理文件路径,将"/"替换成计算机识别的"\"?????sPath?=sPath.replace("/",File.separator);??
- ???File?file?=?newFile(sPath);?????//?路径为文件且不为空则进行删除??
- ???if?(file.isFile()&&?file.exists())?{???????file.delete();??
- ?????flag?=?true;?????}??
- ????returnflag;???}??
执行所有代码完成后,实现了excel的生成,下载以及删除功能,可能实际应用不会如此麻烦,这里只是列出方法,每个功能都可单独使用,举一而反三。