From 4629a2842865565f9b8673beff22030b81ae6220 Mon Sep 17 00:00:00 2001 From: "ian.zhang" Date: Fri, 11 Sep 2020 11:58:21 +0800 Subject: [PATCH 1/4] Fix the contentType issue Raise the PR to fix the sample issue by incorrect content type. By given sample, we will get a xls type excel even set excelType(ExcelTypeEnum.XLSX), which will open with an alert even the file encoding is based on xlsx. "The file format and extension of 'xxx' don't match. The file could be corrupted of unsafe. Unless you trust its source, don't open it. Do you want to open it anyway?" The root cause is from incorrect content type. it will be fine if set the content type based on xlsx as below response.setContentType("vnd.openxmlformats-officedocument.spreadsheetml.sheet") Reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types .xls Microsoft Excel application/vnd.ms-excel .xlsx Microsoft Excel (OpenXML) application/vnd.openxmlformats-officedocument.spreadsheetml.sheet --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9a44127b..1bd0b68d 100644 --- a/README.md +++ b/README.md @@ -80,12 +80,12 @@ DEMO代码地址:[https://github.com/alibaba/easyexcel/blob/master/src/test/ja @GetMapping("download") public void download(HttpServletResponse response) throws IOException { // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman - response.setContentType("application/vnd.ms-excel"); + response.setContentType("vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); - EasyExcel.write(response.getOutputStream(), DownloadData.class).sheet("模板").doWrite(data()); + EasyExcel.write(response.getOutputStream(), DownloadData.class).excelType(ExcelTypeEnum.XLSX).sheet("模板").doWrite(data()); } /** From 006b102e1625ffebaf814ace8e5d86ebda40021a Mon Sep 17 00:00:00 2001 From: "ian.zhang" Date: Fri, 11 Sep 2020 12:05:54 +0800 Subject: [PATCH 2/4] Update correct content type Update correct content type --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1bd0b68d..d7bf60a4 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ DEMO代码地址:[https://github.com/alibaba/easyexcel/blob/master/src/test/ja @GetMapping("download") public void download(HttpServletResponse response) throws IOException { // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman - response.setContentType("vnd.openxmlformats-officedocument.spreadsheetml.sheet"); + response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20"); From 4540aba6997bf6969184712ce8728fb00c80d5de Mon Sep 17 00:00:00 2001 From: "ian.zhang" Date: Fri, 11 Sep 2020 12:20:27 +0800 Subject: [PATCH 3/4] Update WebTest.java Fix incorrect content type. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); --- .../java/com/alibaba/easyexcel/test/demo/web/WebTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java b/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java index ab0ad968..ffa01041 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java @@ -43,12 +43,12 @@ public class WebTest { @GetMapping("download") public void download(HttpServletResponse response) throws IOException { // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman - response.setContentType("application/vnd.ms-excel"); + response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); - EasyExcel.write(response.getOutputStream(), DownloadData.class).sheet("模板").doWrite(data()); + EasyExcel.write(response.getOutputStream(), DownloadData.class).excelType(ExcelTypeEnum.XLSX).sheet("模板").doWrite(data()); } /** @@ -60,13 +60,13 @@ public class WebTest { public void downloadFailedUsingJson(HttpServletResponse response) throws IOException { // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman try { - response.setContentType("application/vnd.ms-excel"); + response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); // 这里需要设置不关闭流 - EasyExcel.write(response.getOutputStream(), DownloadData.class).autoCloseStream(Boolean.FALSE).sheet("模板") + EasyExcel.write(response.getOutputStream(), DownloadData.class).excelType(ExcelTypeEnum.XLSX).autoCloseStream(Boolean.FALSE).sheet("模板") .doWrite(data()); } catch (Exception e) { // 重置response From 2adb7a472e343327bea17a0bf5a1da644d20183a Mon Sep 17 00:00:00 2001 From: "ian.zhang" Date: Fri, 11 Sep 2020 12:26:35 +0800 Subject: [PATCH 4/4] Update WebTest.java Fix missed dependency. --- src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java b/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java index ffa01041..e2c14137 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java @@ -18,6 +18,7 @@ import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.fastjson.JSON; /**