From 475e932aecd335154614016ce0637f04d3f81994 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Fri, 29 Dec 2023 21:45:43 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E5=AF=BC=E5=87=BA=20Excel:Long?= =?UTF-8?q?=20=E7=B1=BB=E5=9E=8B=20ID=20=E4=BC=9A=E4=B8=A2=E5=A4=B1?= =?UTF-8?q?=E7=B2=BE=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iocoder/yudao/framework/excel/core/util/ExcelUtils.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/yudao-framework/yudao-spring-boot-starter-excel/src/main/java/cn/iocoder/yudao/framework/excel/core/util/ExcelUtils.java b/yudao-framework/yudao-spring-boot-starter-excel/src/main/java/cn/iocoder/yudao/framework/excel/core/util/ExcelUtils.java index 1bdeabc50..c2104693e 100644 --- a/yudao-framework/yudao-spring-boot-starter-excel/src/main/java/cn/iocoder/yudao/framework/excel/core/util/ExcelUtils.java +++ b/yudao-framework/yudao-spring-boot-starter-excel/src/main/java/cn/iocoder/yudao/framework/excel/core/util/ExcelUtils.java @@ -1,12 +1,14 @@ package cn.iocoder.yudao.framework.excel.core.util; import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.converters.longconverter.LongStringConverter; import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; import org.springframework.web.multipart.MultipartFile; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.List; /** @@ -33,9 +35,10 @@ public class ExcelUtils { EasyExcel.write(response.getOutputStream(), head) .autoCloseStream(false) // 不要自动关闭,交给 Servlet 自己处理 .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) // 基于 column 长度,自动适配。最大 255 宽度 + .registerConverter(new LongStringConverter()) // 避免 Long 类型丢失精度 .sheet(sheetName).doWrite(data); // 设置 header 和 contentType。写在最后的原因是,避免报错时,响应 contentType 已经被修改了 - response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8")); + response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, StandardCharsets.UTF_8)); response.setContentType("application/vnd.ms-excel;charset=UTF-8"); }