导出Excel通用工具类

导出Excel的两种方法:

一,POI

导入poi包

poi-3.11-beta3-20141111.jar

 /**
*
*/
package com.car.ots.mpckp.utils; import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.commons.beanutils.BeanUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; /**
* @author cora.guo
*
*/
public class ExportUtils { public static void createExcel(HttpServletRequest req,
HttpServletResponse resp, List<Object> datas, String sheetName,
List<String> headers, List<String> colunmValueNames)
throws Exception {
OutputStream out = null;
try {
// 第一步,创建一个webbook,对应一个Excel文件
resp.setContentType("application/vnd.ms-excel");
resp.setCharacterEncoding("UTF-8");
if (req.getHeader("User-Agent").toLowerCase().indexOf("msie") != -) {
resp.setHeader("Content-Disposition", "attachment;filename="
+ URLEncoder.encode(sheetName, "utf-8") + ".xls");
} else {
resp.setHeader("Content-Disposition", "attachment;filename="
+ new String(sheetName.getBytes("utf-8"), "ISO-8859-1")
+ ".xls");
}
resp.setHeader("Cache-Control", "max-age=0");
HSSFWorkbook wb = new HSSFWorkbook(); // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow();
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
// 创建居中样式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFCell cell = null;
if (headers != null && headers.size() > ) {
for (int i = ; i < headers.size(); i++) {
cell = row.createCell(i);
cell.setCellValue(headers.get(i));
cell.setCellStyle(style);
}
}
// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
if (datas != null && datas.size() != ) {
for (int r = ; r < datas.size(); r++) {
Object obj = datas.get(r);
row = sheet.createRow(r+);
if (colunmValueNames != null
&& colunmValueNames.size() != ) {
for (int j = ; j < colunmValueNames.size(); j++) {
String name = colunmValueNames.get(j);
String value = BeanUtils.getProperty(obj, name);
// 创建单元格,设置值
cell = row.createCell(j);
cell.setCellStyle(style);
cell.setCellValue(value);
}
}
}
}
out = resp.getOutputStream();
wb.write(out);
} catch (Exception e) {
// TODO Auto-generated catch block
throw new Exception(e);
} finally {
out.close();
}
}
}

二,JXl

导入包

 package com.car.ots.uis.utils;

 import java.net.URLEncoder;
import java.util.List; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook; import org.apache.commons.beanutils.BeanUtils; public class ExportUtil {
public static void export(HttpServletRequest req, HttpServletResponse resp, List<Object> datas,
String sheetName, List<String> headers, List<String> colunmValueNames) throws Exception {
// 写入excel
WritableWorkbook wb = null;
WritableSheet ws = null;
Label label = null; try {
resp.setContentType("application/vnd.ms-excel");
resp.setCharacterEncoding("UTF-8");
if (req.getHeader("User-Agent").toLowerCase().indexOf("msie") != -) {
resp.setHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode(sheetName, "utf-8") + ".xls");
} else {
resp.setHeader("Content-Disposition",
"attachment;filename=" + new String(sheetName.getBytes("utf-8"), "ISO-8859-1") + ".xls");
}
resp.setHeader("Cache-Control", "max-age=0");
wb = Workbook.createWorkbook(resp.getOutputStream()); ws = wb.createSheet(sheetName, ); WritableFont bold_font = new WritableFont(WritableFont.ARIAL, , WritableFont.BOLD);
WritableCellFormat f = new WritableCellFormat(bold_font);
f.setVerticalAlignment(VerticalAlignment.CENTRE);
f.setAlignment(Alignment.CENTRE);
if(headers!=null&headers.size()!=){
for (int i = ; i < headers.size(); i++) {
ws.setColumnView(i, );
label = new Label(i, , headers.get(i), f);
ws.addCell(label);
}
} WritableFont nobold_font = new WritableFont(WritableFont.ARIAL, , WritableFont.NO_BOLD);
f = new WritableCellFormat(nobold_font);
f.setVerticalAlignment(VerticalAlignment.CENTRE);
f.setAlignment(Alignment.CENTRE);
if(datas!=null&&datas.size()!=){
for (int r = ; r < datas.size(); r++) {
Object obj = datas.get(r);
if (colunmValueNames!=null&&colunmValueNames.size()!=) {
for (int j = ; j < colunmValueNames.size(); j++) {
String name = colunmValueNames.get(j);
String value = BeanUtils.getProperty(obj, name);
label = new Label(j, r + , value, f);
ws.addCell(label);
}
}
}
}
wb.write();
} catch (Exception e) {
throw new Exception(e);
} finally {
wb.write();
wb.close(); }
} }

两者区别:

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

上一篇:javaScript函数提升及作用域


下一篇:extern "c" 的作用