在各类开发中导出报表时常见的需求之一,在java开发中使用apache的poi包可以轻松地完成excel报表导出任务,并且支持excel2007版本。具体代码如下:
public String export2Excel() throws Exception {
String exportFileName = CustomerUtil.getCurrentTime() + "_OrderInfo.xls";
HttpServletResponse response = this.getResponse();
response.setContentType("application/ vnd.ms-excel");
response.addHeader("Content-Disposition", "attachment;filename=" + exportFileName);
//第一步,创建一个WEBBOOK,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
//第二步,在WEBBOOK中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("订单统计表");
HSSFPrintSetup printSetup = sheet.getPrintSetup();
printSetup.setLandscape(true);
sheet.setFitToPage(true);
sheet.setHorizontallyCenter(true);
//第三步,在sheet中添加表头第0行,注意老版本POI对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int)0);
//第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //创建一个居中格式
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setFillBackgroundColor(new HSSFColor.GREY_25_PERCENT().getIndex());
style.setFillForegroundColor(new HSSFColor.GREY_25_PERCENT().getIndex());
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFCell cell = row.createCell((int)0);
cell = row.createCell((int)0);
cell.setCellValue(new HSSFRichTextString("MIJ型号")); cell.setCellStyle(style);
cell = row.createCell((int)1);
cell.setCellValue(new HSSFRichTextString("商品名称")); cell.setCellStyle(style);
cell = row.createCell((int)2);
cell.setCellValue(new HSSFRichTextString("数量")); cell.setCellStyle(style);
cell = row.createCell((int)3);
cell.setCellValue(new HSSFRichTextString("交易额")); cell.setCellStyle(style);
cell = row.createCell((int)4);
cell.setCellValue(new HSSFRichTextString("订单状态")); cell.setCellStyle(style);
cell = row.createCell((int)5);
cell.setCellValue(new HSSFRichTextString("用户名")); cell.setCellStyle(style);
cell = row.createCell((int)6);
cell.setCellValue(new HSSFRichTextString("订单日期")); cell.setCellStyle(style);
cell = row.createCell((int)7);
cell.setCellValue(new HSSFRichTextString("付款方式")); cell.setCellStyle(style);
HSSFCellStyle style_1 = wb.createCellStyle();
style_1.setBorderRight(HSSFCellStyle.BORDER_THIN);
style_1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style_1.setBorderTop(HSSFCellStyle.BORDER_THIN);
style_1.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//第五步,写入实体数据 实际应用中这些数据从数据库得到,
List<OrderStatisticResultVo> list = orderStatisticsBo.findOrderStatisticsInfo(orcVo,getPage(),false);
for(int i=0;i<list.size();i++){
row = sheet.createRow((int)i+1);
OrderStatisticResultVo osrVo = (OrderStatisticResultVo) list.get(i);
//第四步,创建单元格,并设置值
HSSFCell cell_1 = row.createCell((int)0);
cell_1.setCellValue(new HSSFRichTextString(osrVo.getMijType()));cell_1.setCellStyle(style_1);
HSSFCell cell_2 = row.createCell((int)1);
cell_2.setCellValue(new HSSFRichTextString(osrVo.getProductName()));cell_2.setCellStyle(style_1);
HSSFCell cell_3 = row.createCell((int)2);
cell_3.setCellValue(new HSSFRichTextString(osrVo.getDealNumber() + UNIT));cell_3.setCellStyle(style_1);
HSSFCell cell_4 = row.createCell((int)3);
cell_4.setCellValue(new HSSFRichTextString(osrVo.getMoney()));cell_4.setCellStyle(style_1);
HSSFCell cell_5 = row.createCell((int)4);
cell_5.setCellValue(new HSSFRichTextString(CustomerUtil.getOrderStatusName(osrVo.getOrderState()))); cell_5.setCellStyle(style_1);
HSSFCell cell_6 = row.createCell((int)5);
cell_6.setCellValue(new HSSFRichTextString(osrVo.getCustomerId())); cell_6.setCellStyle(style_1);
HSSFCell cell_7 = row.createCell((int)6);
cell_7.setCellValue(new HSSFRichTextString(osrVo.getOrderDate()));cell_7.setCellStyle(style_1);
HSSFCell cell_8 = row.createCell((int)7);
cell_8.setCellValue(new HSSFRichTextString(CustomerUtil.getPayTypeName(osrVo.getPayType()))); cell_8.setCellStyle(style_1);
sheet.setColumnWidth(i, 256 * 20);
}
//第六步,将文件存到指定位置
try {
wb.write(response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
return NONE;
}
前面4行语句是当点击导出按钮后,弹出文件下载消息框,点击保存后弹出文件保存对话框。第一步到第四步设置表格头部列名,以及格式。第五步执行查询方法,得到要遍历的集合,最后保存文件。