在平常的项目中,我们总会遇到导出数据到excel表中这种情况,网上也有很多类似的方法,下面是我自己根据网上总结的一个导出数据的方法:
需要的jar包:poi-3.9.jar,poi-excelant-3.9.jar,poi-ooxml-3.9.jar,poi-ooxml-schemas-3.9.jar,poi-scratchpad-3.9.jar
jar包准备完毕,那么我们贴代码:
这是基本的工具的代码:
package com.cn.uitrs.common.utils;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
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;
import com.jfinal.kit.StrKit;
/**
* @fileName 类名:POIUtils.java
* @author 作者:BealHe
* @date 时间:2018年8月20日 上午11:36:55
* @explain 类说明:导出Excel表工具
*/
public class POIUtils {
public static boolean createFixationSheet(OutputStream os, List<Map<String, String>> list) throws IOException {
// 创建工作薄
HSSFWorkbook wb = new HSSFWorkbook();
// 在工作薄上建一张工作表
HSSFSheet sheet = wb.createSheet();
HSSFRow row = sheet.createRow((short) 0);
sheet.createFreezePane(0, 1);
cteateCell(wb, row, (short) 0, "工号");
cteateCell(wb, row, (short) 1, "姓名");
cteateCell(wb, row, (short) 2, "部门");
cteateCell(wb, row, (short) 3, "采样总价");
cteateCell(wb, row, (short) 4, "分析总价");
cteateCell(wb, row, (short) 5, "编制总价");
cteateCell(wb, row, (short) 6, "合计");
for (int i = 0; i < list.size();) {
Map<String, String> map = list.get(i++);
HSSFRow rowi = sheet.createRow((short) (i));
String userId = StrKit.isBlank(map.get("userId")) ? "" : map.get("userId");
cteateCell(wb, rowi, (short) 0, userId);
String realName = StrKit.isBlank(map.get("realName")) ? "" : map.get("realName");
cteateCell(wb, rowi, (short) 1, realName);
String depart = StrKit.isBlank(map.get("depart")) ? "" : map.get("depart");
cteateCell(wb, rowi, (short) 2, depart);
String sample = StrKit.isBlank(map.get("sample")) ? "" : map.get("sample");
cteateCell(wb, rowi, (short) 3, sample);
String test = StrKit.isBlank(map.get("test")) ? "" : map.get("test");
cteateCell(wb, rowi, (short) 4, test);
String report = StrKit.isBlank(map.get("report")) ? "" : map.get("report");
cteateCell(wb, rowi, (short) 5, report);
String heji = StrKit.isBlank(map.get("heji")) ? "" : map.get("heji");
cteateCell(wb, rowi, (short) 6, heji);
}
wb.write(os);
os.flush();
os.close();
System.out.println("绩效文件生成成功!");
return true;
}
private static void cteateCell(HSSFWorkbook wb, HSSFRow row, short col, String val) {
@SuppressWarnings("deprecation")
HSSFCell cell = row.createCell(col);
cell.setCellValue(val);
HSSFCellStyle cellstyle = wb.createCellStyle();
cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION);
cell.setCellStyle(cellstyle);
}
}
基本工具是这样的,其实很简单,网上类似工具都是差不多的,这也是我自己根据网上总结的,可能没有别人的那么好。
然后是调用工具的方法:
// Excel文件名
String name = String.valueOf(new Date().getTime());
HttpServletResponse response = getResponse();
// 取得输出流
OutputStream os = response.getOutputStream();
response.reset();
// 设定输出文件头,该方法有两个参数,分别表示应答头的名字和值。
response.setHeader("Content-disposition", "attachment; filename=" + name + ".xls");
response.setContentType("application/msexcel");
POIUtils.createFixationSheet(os, result);
result里面的数据什么的,我就没有给出了,这个大家如果用这个方法的话,随便加点数据测试就可以了。由于我使用的是JFinal的框架,所以获取HttpServletResponse的方式是这样的,大家自己获取HttpServletResponse时根据自己实际情况来定。
下面是导出数据结果: