导出Excel的两种方法:
一,POI
导入poi包
poi-3.11-beta3-20141111.jar
1 /** 2 * 3 */ 4 package com.car.ots.mpckp.utils; 5 6 import java.io.OutputStream; 7 import java.net.URLEncoder; 8 import java.util.List; 9 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 import org.apache.commons.beanutils.BeanUtils; 14 import org.apache.poi.hssf.usermodel.HSSFCell; 15 import org.apache.poi.hssf.usermodel.HSSFCellStyle; 16 import org.apache.poi.hssf.usermodel.HSSFRow; 17 import org.apache.poi.hssf.usermodel.HSSFSheet; 18 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 19 20 /** 21 * @author cora.guo 22 * 23 */ 24 public class ExportUtils { 25 26 public static void createExcel(HttpServletRequest req, 27 HttpServletResponse resp, List<Object> datas, String sheetName, 28 List<String> headers, List<String> colunmValueNames) 29 throws Exception { 30 OutputStream out = null; 31 try { 32 // 第一步,创建一个webbook,对应一个Excel文件 33 resp.setContentType("application/vnd.ms-excel"); 34 resp.setCharacterEncoding("UTF-8"); 35 if (req.getHeader("User-Agent").toLowerCase().indexOf("msie") != -1) { 36 resp.setHeader("Content-Disposition", "attachment;filename=" 37 + URLEncoder.encode(sheetName, "utf-8") + ".xls"); 38 } else { 39 resp.setHeader("Content-Disposition", "attachment;filename=" 40 + new String(sheetName.getBytes("utf-8"), "ISO-8859-1") 41 + ".xls"); 42 } 43 resp.setHeader("Cache-Control", "max-age=0"); 44 HSSFWorkbook wb = new HSSFWorkbook(); 45 46 // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet 47 HSSFSheet sheet = wb.createSheet(sheetName); 48 // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short 49 HSSFRow row = sheet.createRow(0); 50 // 第四步,创建单元格,并设置值表头 设置表头居中 51 HSSFCellStyle style = wb.createCellStyle(); 52 // 创建居中样式 53 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); 54 HSSFCell cell = null; 55 if (headers != null && headers.size() > 0) { 56 for (int i = 0; i < headers.size(); i++) { 57 cell = row.createCell(i); 58 cell.setCellValue(headers.get(i)); 59 cell.setCellStyle(style); 60 } 61 } 62 // 第五步,写入实体数据 实际应用中这些数据从数据库得到, 63 if (datas != null && datas.size() != 0) { 64 for (int r = 0; r < datas.size(); r++) { 65 Object obj = datas.get(r); 66 row = sheet.createRow(r+1); 67 if (colunmValueNames != null 68 && colunmValueNames.size() != 0) { 69 for (int j = 0; j < colunmValueNames.size(); j++) { 70 String name = colunmValueNames.get(j); 71 String value = BeanUtils.getProperty(obj, name); 72 // 创建单元格,设置值 73 cell = row.createCell(j); 74 cell.setCellStyle(style); 75 cell.setCellValue(value); 76 } 77 } 78 } 79 } 80 out = resp.getOutputStream(); 81 wb.write(out); 82 } catch (Exception e) { 83 // TODO Auto-generated catch block 84 throw new Exception(e); 85 } finally { 86 out.close(); 87 } 88 } 89 }
二,JXl
导入包
1 package com.car.ots.uis.utils; 2 3 import java.net.URLEncoder; 4 import java.util.List; 5 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 import jxl.Workbook; 10 import jxl.format.Alignment; 11 import jxl.format.VerticalAlignment; 12 import jxl.write.Label; 13 import jxl.write.WritableCellFormat; 14 import jxl.write.WritableFont; 15 import jxl.write.WritableSheet; 16 import jxl.write.WritableWorkbook; 17 18 import org.apache.commons.beanutils.BeanUtils; 19 20 public class ExportUtil { 21 public static void export(HttpServletRequest req, HttpServletResponse resp, List<Object> datas, 22 String sheetName, List<String> headers, List<String> colunmValueNames) throws Exception { 23 // 写入excel 24 WritableWorkbook wb = null; 25 WritableSheet ws = null; 26 Label label = null; 27 28 try { 29 resp.setContentType("application/vnd.ms-excel"); 30 resp.setCharacterEncoding("UTF-8"); 31 if (req.getHeader("User-Agent").toLowerCase().indexOf("msie") != -1) { 32 resp.setHeader("Content-Disposition", 33 "attachment;filename=" + URLEncoder.encode(sheetName, "utf-8") + ".xls"); 34 } else { 35 resp.setHeader("Content-Disposition", 36 "attachment;filename=" + new String(sheetName.getBytes("utf-8"), "ISO-8859-1") + ".xls"); 37 } 38 resp.setHeader("Cache-Control", "max-age=0"); 39 wb = Workbook.createWorkbook(resp.getOutputStream()); 40 41 ws = wb.createSheet(sheetName, 0); 42 43 WritableFont bold_font = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD); 44 WritableCellFormat f = new WritableCellFormat(bold_font); 45 f.setVerticalAlignment(VerticalAlignment.CENTRE); 46 f.setAlignment(Alignment.CENTRE); 47 if(headers!=null&headers.size()!=0){ 48 for (int i = 0; i < headers.size(); i++) { 49 ws.setColumnView(i, 25); 50 label = new Label(i, 0, headers.get(i), f); 51 ws.addCell(label); 52 } 53 } 54 55 WritableFont nobold_font = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD); 56 f = new WritableCellFormat(nobold_font); 57 f.setVerticalAlignment(VerticalAlignment.CENTRE); 58 f.setAlignment(Alignment.CENTRE); 59 if(datas!=null&&datas.size()!=0){ 60 for (int r = 0; r < datas.size(); r++) { 61 Object obj = datas.get(r); 62 if (colunmValueNames!=null&&colunmValueNames.size()!=0) { 63 for (int j = 0; j < colunmValueNames.size(); j++) { 64 String name = colunmValueNames.get(j); 65 String value = BeanUtils.getProperty(obj, name); 66 label = new Label(j, r + 1, value, f); 67 ws.addCell(label); 68 } 69 } 70 } 71 } 72 wb.write(); 73 } catch (Exception e) { 74 throw new Exception(e); 75 } finally { 76 wb.write(); 77 wb.close(); 78 79 } 80 } 81 82 }
两者区别:
POI为apache公司的一个子项目,主要是提供一组操作windows文档的Java API
JavaExcel俗称jxl是一开放源码项目,通过它开发人员可以读取Excel文件的内容、创建新的Excel文件、更新已经存在的Excel文件。使用该API非Windows操作系统也可以通过纯Java应用来处理Excel数据表。因为是使用Java编写的,所以我们在Web应用中可以通过JSP、Servlet来调用API实现对Excel数据表的访问。
JVM虚拟机内存消耗的情况:
数据量3000条数据,每条60列.JVM虚拟机内存大小64M.
使用POI:运行到2800条左右就报内存溢出.
使用JXL:3000条全部出来,并且内存还有21M的空间.
可想而知,在对内存的消耗方面差距还是挺大的.
也许是由于JXL在对资源回收利用方面做的不错.
效率方面:
也是基于大数据量而言的,数据量小的话基本上差别不大,也不难被发觉.但是大的数据量,POI消耗的JVM内存远比JXL消耗的多.但相比提供的功能的话,JXL又相对弱了点.所以如果要实现的功能比较复杂的情况下可以考虑使用POI,但如果只想生成一些大数据量可以考虑使用JXL