Commit 475e932aecd335154614016ce0637f04d3f81994
1 parent
08613727
🐛 导出 Excel:Long 类型 ID 会丢失精度
Showing
1 changed file
with
4 additions
and
1 deletions
yudao-framework/yudao-spring-boot-starter-excel/src/main/java/cn/iocoder/yudao/framework/excel/core/util/ExcelUtils.java
| 1 | 1 | package cn.iocoder.yudao.framework.excel.core.util; |
| 2 | 2 | |
| 3 | 3 | import com.alibaba.excel.EasyExcel; |
| 4 | +import com.alibaba.excel.converters.longconverter.LongStringConverter; | |
| 4 | 5 | import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; |
| 5 | 6 | import org.springframework.web.multipart.MultipartFile; |
| 6 | 7 | |
| 7 | 8 | import jakarta.servlet.http.HttpServletResponse; |
| 8 | 9 | import java.io.IOException; |
| 9 | 10 | import java.net.URLEncoder; |
| 11 | +import java.nio.charset.StandardCharsets; | |
| 10 | 12 | import java.util.List; |
| 11 | 13 | |
| 12 | 14 | /** |
| ... | ... | @@ -33,9 +35,10 @@ public class ExcelUtils { |
| 33 | 35 | EasyExcel.write(response.getOutputStream(), head) |
| 34 | 36 | .autoCloseStream(false) // 不要自动关闭,交给 Servlet 自己处理 |
| 35 | 37 | .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) // 基于 column 长度,自动适配。最大 255 宽度 |
| 38 | + .registerConverter(new LongStringConverter()) // 避免 Long 类型丢失精度 | |
| 36 | 39 | .sheet(sheetName).doWrite(data); |
| 37 | 40 | // 设置 header 和 contentType。写在最后的原因是,避免报错时,响应 contentType 已经被修改了 |
| 38 | - response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8")); | |
| 41 | + response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, StandardCharsets.UTF_8)); | |
| 39 | 42 | response.setContentType("application/vnd.ms-excel;charset=UTF-8"); |
| 40 | 43 | } |
| 41 | 44 | ... | ... |