根据模板导出excel

 1     @RequestMapping(value = "/export", method = RequestMethod.GET)
 2     public void exportApprovalsLog(
 3             @RequestParam(value = "department", required = false) String department,
 4             @RequestParam(value = "approver_ids", required = false) List<String> approverIds,
 5             @RequestParam(value = "start_time", required = false) Long startTime,
 6             @RequestParam(value = "end_time", required = false) Long endTime,
 7             @RequestParam(value = "case_code", required = false) String caseCode,
 8             HttpServletRequest request, HttpServletResponse response) {
 9 
10         if (CollectionUtils.isEmpty(approverIds)) {
11             approverIds = ImmutableList.of("777");
12         }
13 
14         List<ApproveLog> list = service.getApprovalsLog(department, approverIds, startTime, endTime, caseCode, null,null);
15 
16         try {
17             String path = request.getServletContext().getRealPath("")
18                     + "/excelTemplate/审核记录.xls";   //这个是我的excel模板 根目录是wabapp
19             InputStream in = new FileInputStream(new File(path));
20             HSSFWorkbook work = new HSSFWorkbook(in);
21             HSSFSheet sheet = work.getSheetAt(0);  // 获取sheet
22             // 插入数据
23             for (int i = 1; i < list.size() + 1; i++) { // 表头占1行 i=1; 表头占2行 i=2
24                 ApproveLog log = list.get(i - 1);
25                 HSSFRow row = sheet.createRow(i);
26 
27                 HSSFCell cell = row.createCell(0);
28                 cell.setCellValue(log.getCaseId());
29                 cell1 = row.createCell(1);
30                 cell.setCellValue(log.getCaseName());
31                 // 其他数据
32             }
33 
34             String fileName = ExportExcelUtils.makeFileName("审核记录", department,
35                     ExportExcelUtils.getNamesByIds(constantService, approverIds), startTime, endTime);
36             OutputStream os = response.getOutputStream();// 取得输出流
37             response.setCharacterEncoding("UTF-8");
38             response.setContentType("application/vnd.ms-excel");
39             response.setHeader("Content-disposition",
40                     "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "iso8859-1") + ".xls"); // 防止中文乱码
41             work.write(os);
42             os.close();
43         } catch (FileNotFoundException e) {
44             throw new BizException(ErrorCode.DATA_NOT_EXIST, "文件路径错误");
45         } catch (IOException e) {
46             e.printStackTrace();
47             throw new BizException(ErrorCode.DATA_NOT_EXIST, "文件输入流错误");
48         }
49     }
原文地址:https://www.cnblogs.com/wihainan/p/6113478.html