读书人

java导出Excel文件以及文件的下载、剔

发布时间: 2014-01-14 23:14:00 作者: rapoo

java导出Excel文件以及文件的下载、删除

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

1、创建Excel文件:
?

[java] view plaincopy
  1. String?fileName?=?"C:\\统计数据"+startDate2+"-"+endDate2+".xls";???WritableWorkbook?workbook?=Workbook.createWorkbook(new?File(fileName));??

2、创建数据样式:
[java] view plaincopy
  1. //字体:??//字体大小,字体类型。??
  2. WritableFont??wr?=?newWritableFont(WritableFont.TIMES,14,WritableFont.BOLD);??//创建字体样式引用??
  3. WritableCellFormat?fontFormat1?=?new?WritableCellFormat(wr);??//对其方式??
  4. fontFormat1.setAlignment(jxl.format.Alignment.CENTRE);??//是否有边框??
  5. fontFormat1.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);???????????????
  6. //时间:??jxl.write.DateFormat?df?=?newjxl.write.DateFormat("yyyy-MM-dd");??
  7. WritableCellFormat?dateFormat?=?new?WritableCellFormat(df);??dateFormat.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);??
  8. dateFormat.setAlignment(jxl.format.Alignment.CENTRE);????
  9. //数字:??jxl.write.NumberFormat?nf?=?newjxl.write.NumberFormat("###,###,###,###,###");??
  10. WritableCellFormat?numberFormat?=?new?WritableCellFormat(nf);??numberFormat.setAlignment(jxl.format.Alignment.CENTRE);??
  11. numberFormat.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);???//建立第N个sheet??
  12. WritableSheet?sheet?=?workbook.createSheet("名称",N);?????//sheet的第几列,和列宽。??
  13. sheet.setColumnView(0,6);??sheet.setColumnView(1,14);??
  14. sheet.setColumnView(2,12);?????//合并单元格(起始列,起始行,结束列,结束行)??
  15. sheet.mergeCells(1,0,2,0);????//向sheet中写入数据(列,行,内容,格式)??
  16. Label?label0?=?new?Label(1,0,"1992-04-06",dateFormat);?????????????sheet.addCell(label0);??
  17. Label?label2?=?new?Label(1,1,"许阳",fontFormat2);?????????????sheet.addCell(label2);??
  18. Label?label3?=?new?Label(2,1,"21",numberFormat1);?????????????sheet.addCell(label3);??
  19. workbook.write();??workbook.close();??


以上excel写入数据完成,并保存到了指定的位置。若是在本地操作,则没问题,若是在服务器上,就得使用java的输入输出流写入到本地(下载)。
调用downLoadFile方法。将文件位置用参数传过去。
[java] view plaincopy
  1. this.downLoadFile(fileName);??

this.downLoadFile(fileName);

downLoadFile方法:
[java] view plaincopy
  1. public?void?downLoadFile(String?filePth)?{?????????HttpServletResponse?response?=ServletActionContext.getResponse();??
  2. ???????HttpServletRequest?request?=ServletActionContext.getRequest();?????????try?{??
  3. ?????????????//得到当前路径??
  4. ???????????//StringfilePath=request.getSession().getServletContext().getRealPath(File.separator);?????????????File?temFile?=?new?File(filePth);??
  5. ??????????//判断文件是否存在?????????????if(!temFile.exists()){??
  6. ???????????????response.getWriter().write("ERROR:File?Not?Found");?????????????????return?;??
  7. ???????????}????????????//处理文件名得位置(若服务器为linux和windows的处理方法不同)??
  8. ???????????String?fileName?=filePth.substring(filePth.lastIndexOf(File.separator)+1);???????????//设置头文件,名称和内容的编码不同,否则会出现乱码。??
  9. ???????????response.setHeader("Content-Disposition",?"attachment;?filename="+new?String((fileName).getBytes("gbk"),"iso8859-1"));????
  10. ???????????response.setContentType("application/x-download");?????????????OutputStream?ot=response.getOutputStream();??
  11. ???????????BufferedInputStream?bis??=?newBufferedInputStream(new?FileInputStream(temFile));?????????????BufferedOutputStream?bos?=?new?BufferedOutputStream(ot);??
  12. ???????????byte[]?buffer?=?new?byte[4096];?????????????int?length?=?0;??
  13. ???????????while((length?=?bis.read(buffer))?>?0){?????????????????bos.write(buffer,0,length);??
  14. ???????????}?????????????bos.close();??
  15. ???????????bis.close();?????????????ot.close();??
  16. ???????}?catch?(Exception?e)?{?????????????e.printStackTrace();??
  17. ???????}??????}??



此时就会下载完成,完成后需要在服务器删除此Excel文件:
在this.downLoadFile方法后面继续调用删除的方法:
[java] view plaincopy
  1. public?boolean?deleteFile(String?sPath)?{?????boolean?flag?=?false;??
  2. ??//处理文件路径,将"/"替换成计算机识别的"\\"?????sPath?=sPath.replace("/",File.separator);??
  3. ???File?file?=?newFile(sPath);?????//?路径为文件且不为空则进行删除??
  4. ???if?(file.isFile()&&?file.exists())?{???????file.delete();??
  5. ?????flag?=?true;?????}??
  6. ????returnflag;???}??



执行所有代码完成后,实现了excel的生成,下载以及删除功能,可能实际应用不会如此麻烦,这里只是列出方法,每个功能都可单独使用,举一而反三。

读书人网 >编程

热点推荐