使用 POI操作Excel表格导出
@RequestMapping("/exportBusinessReport")
public Result exportBusinessReport(HttpServletRequest request, HttpServletResponse response){
try{
Map<String, Object> result = reportService.getBusinessReport();
String reportDate = (String) result.get("reportDate");
Integer todayNewMember = (Integer) result.get("todayNewMember");
List<Map> hotSetmeal = (List<Map>) result.get("hotSetmeal");
String temlateRealPath = request.getSession().getServletContext().getRealPath("template") +
File.separator + "report_template.xlsx";
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(temlateRealPath)));
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFRow row = sheet.getRow(2);
row.getCell(5).setCellValue(reportDate);
row = sheet.getRow(4);
row.getCell(5).setCellValue(todayNewMember);
row.getCell(7).setCellValue(totalMember);
row = sheet.getRow(5);
row.getCell(5).setCellValue(thisWeekNewMember);
row.getCell(7).setCellValue(thisMonthNewMember);
int rowNum = 12;
for(Map map : hotSetmeal){
String name = (String) map.get("name");
Long setmeal_count = (Long) map.get("setmeal_count");
BigDecimal proportion = (BigDecimal) map.get("proportion");
row = sheet.getRow(rowNum ++);
row.getCell(4).setCellValue(name);
row.getCell(5).setCellValue(setmeal_count);
row.getCell(6).setCellValue(proportion.doubleValue());
}
ServletOutputStream out = response.getOutputStream();
response.setContentType("application/vnd.ms-excel");
response.setHeader("content-Disposition", "attachment;filename=report.xlsx");
workbook.write(out);
out.flush();
out.close();
workbook.close();
return null;
}catch (Exception e){
return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL,null);
}
}