版本:2.2.10
Controller:
@ApiOperation(value = "统计分析:导出") @RequestMapping(value = "exportCountyResultList", method = RequestMethod.GET) public void exportCountyResultList(HttpServletRequest request,HttpServletResponse response) throws Exception { String authToken = (request.getParameter("token") != null) ? request.getParameter("token") : request.getHeader("Authorization"); String userId = BusinessJWTTokenUtil.getUsernameFromToken(authToken); String fileName = System.currentTimeMillis() + ".xlsx"; reportService.exportCountyResultList(response,userId, fileName); }
Service:
`public void exportCountyResultList(HttpServletResponse response, String userId, String fileName) throws CustomException, IOException {
OutputStream outputStream = new FileOutputStream(filePath);
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
// 处理中文乱码
String encode = URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString());
response.setHeader("Content-disposition", "attachment;filename=" + encode + ExcelTypeEnum.XLSX.getValue());
EasyExcel.write(response.getOutputStream())
.registerWriteHandler(new MyMergeStrategy(cellRangeAddressList))
.registerWriteHandler(new SimpleColumnWidthStyleStrategy(20))
.registerWriteHandler(writeColorHandler).head(headerList)
.autoCloseStream(Boolean.TRUE).sheet("导出数据").doWrite(listO);
}`
Wps打不开,PostMan调用乱码