生成Excel文件
@Autowired
private WayBillService wayBillService;
@Action("report_exportXls")
public String exportXsl() throws Exception{
//
List<WayBill> wayBills = wayBillService.findWayBills(model);
//生成Excel文件
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
HSSFSheet sheet = hssfWorkbook.createSheet("運單數據");
//表頭
HSSFRow headRow = sheet.createRow(0);
headRow.createCell(0).setCellValue("運單號");
headRow.createCell(1).setCellValue("寄件人");
headRow.createCell(2).setCellValue("寄件人電話");
headRow.createCell(3).setCellValue("寄件人地址");
headRow.createCell(4).setCellValue("收件人");
headRow.createCell(5).setCellValue("收件人電話");
headRow.createCell(6).setCellValue("收件人地址");
//表格數據
for (WayBill wayBill : wayBills) {
HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum()+1);
dataRow.createCell(0).setCellValue(wayBill.getWayBillNum());
dataRow.createCell(1).setCellValue(wayBill.getSendName());
dataRow.createCell(2).setCellValue(wayBill.getSendMobile());
dataRow.createCell(3).setCellValue(wayBill.getSendAddress());
dataRow.createCell(4).setCellValue(wayBill.getRecName());
dataRow.createCell(5).setCellValue(wayBill.getRecMobile());
dataRow.createCell(6).setCellValue(wayBill.getRecAddress());
}
//下載導出
//設置頭信息
ServletActionContext.getResponse().setContentType("application/vnd.ms-excel");
String filename = "運單數據.xls";
String agent = ServletActionContext.getRequest().getHeader("user-agent");
filename = FileUtils.encodeDownloadFilename(filename, agent);
ServletActionContext.getResponse().setHeader("Content-Disposition",
"attachment;filename="+filename);
ServletOutputStream outputStream = ServletActionContext.getResponse().getOutputStream();
hssfWorkbook.write(outputStream);
//關閉
hssfWorkbook.close();
return NONE;
}
生成Excel文件