JXL【已过时】
A Java library for reading/writing Excel - Browse Files at SourceForge.net
https://sourceforge.net/projects/jexcelapi/files/
alibaba easyexcel
alibaba/easyexcel
https://github.com/alibaba/easyexcel/
读Excel | Easy Excel
https://easyexcel.opensource.alibaba.com/docs/current/quickstart/read
写Excel | Easy Excel
https://easyexcel.opensource.alibaba.com/docs/current/quickstart/write
记录
指定下载文件的文件名
关键代码看懂一下三个对 resopnse 的设置即可
1 2 3 4
| response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); String fileName = URLEncoder.encode(DeviceEnum.getDescByName(deviceTypeName) + "-模板", "UTF-8").replaceAll("\\+", "%20"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
|
完整示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
@GetMapping("/download/{devType}") public void download(@PathVariable("devType") String deviceTypeName, HttpServletResponse response) { final List<List<String>> head = devTypeService.obtainExcelTemplateHead(deviceTypeName); log.info(JSON.toJSONString(head)); try { List<List<Object>> excelDataList = Collections.singletonList(Collections.emptyList()); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); String fileName = URLEncoder.encode(DeviceEnum.getDescByName(deviceTypeName) + "-模板", "UTF-8").replaceAll("\\+", "%20"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); EasyExcel.write(response.getOutputStream()).head(head).needHead(true).sheet("模板").doWrite(excelDataList); } catch (IOException e) { log.warn("download 设备 IOException", e); } }
|
遇到过的问题
导出不带表头的 excel 模板
1 2 3
| List<List<Object>> excelDataList = Collections.singletonList(Collections.emptyList()); EasyExcel.write(response.getOutputStream()).head(head).needHead(true).sheet("模板").doWrite(excelDataList);
|