使用ExcelWriter导出数据到Excel

1.引入hutool的pom依赖

  <dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.1.4</version>
  </dependency>

 2.工具类代码

 1 public class ExcelExportUtils {
 2 
 3     public static void  exportExcelByResponse(ExcelWriter excelWriter, HttpServletResponse response, String fileName, HttpServletRequest request) throws IOException {
 4         String userAgent = request.getHeader("USER-AGENT");
 5         response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
 6             if(StringUtils.contains(userAgent, "MSIE")||StringUtils.contains(userAgent, "Trident") || StringUtils.contains(userAgent,"Edge")){//IE 浏览器
 7                 fileName = URLEncoder.encode(fileName,"UTF8");
 8             }else{//火狐,google等其他浏览器
 9                 fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
10             }
11             response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
12             //客户端不缓存
13             response.setCharacterEncoding("UTF-8");
14             response.addHeader("Pargam", "no-cache");
15             response.addHeader("Cache-Control", "no-cache");
16             Workbook workbook = excelWriter.getWorkbook();
17             ServletOutputStream outputStream = response.getOutputStream();
18             workbook.write(outputStream);
19             workbook.close();
20             outputStream.close();
21     }
22 }

3.业务调用

 1 @Override
 2 public void exportDailyReportDetailExcel(ReportDetailVO reportDetailVO, HttpServletResponse response, HttpServletRequest request) {
 3         ExcelWriter bigWriter = ExcelUtil.getBigWriter();
 4         try {
 5             // 导出数据
 6             List<ReportDetailVO> reportDetailVOList = statisticalReportMapper.getDailyReportDetailList(reportDetailVO);
 7             // 设置列宽
 8             bigWriter.setColumnWidth(0, 15);
 9             bigWriter.setColumnWidth(1, 10);
10             bigWriter.setColumnWidth(2, 10);
11             bigWriter.setColumnWidth(3, 20);
12             bigWriter.setColumnWidth(4, 20);
13             bigWriter.setColumnWidth(5, 15);
14             // 导出列名,对应实体类属性名
15             bigWriter.addHeaderAlias("companyName", "单位");
16             bigWriter.addHeaderAlias("deptName", "部门");
17             bigWriter.addHeaderAlias("nickName", "姓名");
18             bigWriter.addHeaderAlias("idCard", "身份证");
19             bigWriter.addHeaderAlias("address", "身份证地址");
20             bigWriter.addHeaderAlias("phone", "联系电话");
21             bigWriter.setOnlyAlias(true);
22             bigWriter.write(reportDetailVOList);
23         } catch (Exception e) {
24             e.printStackTrace();
25             response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
26         }
27         try {
28             ExcelExportUtils.exportExcelByResponse(bigWriter, response, "报备统计" + reportDetailVO.getSearchDate(), request);
29         } catch (IOException e) {
30             throw new BaseFrameException(1, "导出失败", false);
31         }
32     }
33 }

 

  
上一篇:php应用容器workerman_worker类接口stopAll说明及范例


下一篇:SpringBoot整合shiro系列-SpingBoot是如何将shiroFilter注册到servlet容器中的