在开发工程中,经常会遇到需要导出数据为excel,下面是我开发中的一些总结,以备遗忘,项目框架是SSH。
1、首先在action中创建导出excel的方法。
public String exportExcel(){
log.debug("导出excel方法开始:");
try {
//List list2 = service.getCheckList(sql, null);
//List list2 = (List) session.getAttribute("export_list");
//上面的list表示你要导出到excel的数据
//使用poi下载文件
HSSFWorkbook workbook = new HSSFWorkbook();
//创建sheet
HSSFSheet sheet1 = workbook.createSheet("sheet1");
//创建row信息
HSSFRow row = sheet1.createRow(0);
//设置每列宽度
sheet1.setColumnWidth(0, 35*256);
sheet1.setColumnWidth(1, 25*256);
sheet1.setColumnWidth(2, 15*256);
sheet1.setColumnWidth(3, 20*256);
// sheet1.setColumnWidth(4, 20*256);
// sheet1.setColumnWidth(5, 18*256);
// sheet1.setColumnWidth(6, 11*256);
// sheet1.setColumnWidth(7, 20*256);
// sheet1.setColumnWidth(8, 15*256);
//sheet1.setColumnWidth(9, 25*256);
//创建单元格头标
// row.createCell(0).setCellValue("报告单编号");
// row.createCell(1).setCellValue("车牌号码");
// row.createCell(2).setCellValue("号牌种类");
// row.createCell(3).setCellValue("检测站名称");
// row.createCell(4).setCellValue("检测日期");
// row.createCell(5).setCellValue("检测方法");
// row.createCell(6).setCellValue("检测结果");
// row.createCell(7).setCellValue("报告单核验日期");
// row.createCell(8).setCellValue("报告单核验人员");
// row.createCell(9).setCellValue("审核结果");
row.createCell(0).setCellValue("检测站名称");
row.createCell(1).setCellValue("报告单编号");
row.createCell(2).setCellValue("报告单日期");
row.createCell(3).setCellValue("报告单签发年份");
//获取数据
for (int i = 0; i < list2.size(); i++) {
if (list2 != null && list2.size() != 0) {
Object[] objs = (Object[]) list2.get(i);
int lastRowNum = sheet1.getLastRowNum();
HSSFRow lastRow = sheet1.createRow(lastRowNum + 1);
lastRow.createCell(0).setCellValue(objs[0]==null ? "":objs[0].toString());
lastRow.createCell(1).setCellValue(objs[1]==null ? "":objs[1].toString());
lastRow.createCell(2).setCellValue(objs[2]==null ? "":objs[2].toString());
lastRow.createCell(3).setCellValue(objs[3]==null ? "":objs[3].toString());
// lastRow.createCell(4).setCellValue(objs[6]==null ? "":objs[6].toString());
// lastRow.createCell(5).setCellValue(objs[6]==null ? "":objs[6].toString());
// lastRow.createCell(6).setCellValue(objs[6]==null ? "":objs[6].toString());
// lastRow.createCell(7).setCellValue(objs[6]==null ? "":objs[6].toString());
// lastRow.createCell(8).setCellValue(objs[6]==null ? "":objs[6].toString());
// lastRow.createCell(9).setCellValue(objs[9].toString());
}
}
//response文件流
HttpServletResponse response = ServletActionContext.getResponse();
//设置文件名
SimpleDateFormat sdfo = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String filename = sdfo.format(new Date()) + ".xls";
//设置文件输出头
response.addHeader("Content-Disposition", "attachment;filename=" + filename);
OutputStream out = response.getOutputStream();
workbook.write(out);
out.close();
//response.setHeader("Content-Disposition", "attachment;filename="+MyFileUtils.encodeDownloadFilename(filename, request.getHeader("user-agent")));
//设置文件类型servletAction.getMine
//ServletContext servletContext = ServletActionContext.getServletContext();
//response.setContentType(servletContext.getMimeType(filename));
//下载输出流
//workbook.write(response.getOutputStream());
} catch (Exception e) {
log.debug("导出excel异常:");
e.printStackTrace();
}
log.debug("导出excel结束");
return NONE;
}
2、jsp页面一开始使用ajax方式调用的该方法,出现不报错,但是也不提示下载信息。
原因:java导出excel中不能使用ajax方式提交;
解决:使用的是window.location.href 方式请求到action,可以正常下载。
写的不好不够完善,算是自己的记事本防止自己忘掉流程吧!!
参考资料:
https://blog.youkuaiyun.com/wangchangpen62/article/details/44410967