[alibaba/easyexcel]版本3.0.5读取csv格式的Excel文档问题

2024-05-16 768 views
9

建议先去看文档 快速开始常见问题 触发场景描述 csv格式的excel文档,当编码为utf-8的时候内容能读取到,编码为GB2312的时候读取不到数据 触发Bug的代码


   这里写代码
```无需代码
**提示的异常或者没有达到的效果**
![GB2312](https://user-images.githubusercontent.com/43319760/154241413-fc770488-eaea-4897-9739-a42417512270.jpg)
![UTF-8](https://user-images.githubusercontent.com/43319760/154241443-07dddcbb-a06e-4910-bcd0-e892fa34b487.jpg)

回答

7

版本3.0.5,我也是读取GB2312编码的CSV,读取到了,但是乱码,在读取时能否指定文件编码

09:01:51.662 DEBUG : 解析到一条数据:{"amount":19754.2,"consumerCount":318,"merchantFullName":"�ܼ�","merchantId":"�ܼ�","merchantName":"�ܼ","transactionCount":352,"transactionDate":1642694400000,"wxPromoterId":0,"wxPromoterTeam":"r05911��"}

8

Hello Mr Zhuang, we are a school group for Software Engineering. We want to offer to try to fix or develop this issue, or at least do a little contribution! Hope to get your consent and support!

8

版本3.0.5,我也是读取GB2312编码的CSV,读取到了,但是乱码,在读取时能否指定文件编码

09:01:51.662 DEBUG : 解析到一条数据:{"amount":19754.2,"consumerCount":318,"merchantFullName":"�ܼ�","merchantId":"�ܼ�","merchantName":"�ܼ","transactionCount":352,"transactionDate":1642694400000,"wxPromoterId":0,"wxPromoterTeam":"r05911��"}

easycsv底层用的apache的commons-csv,你可以直接使用commons-csv在读取的时候设置编码,或者把你的用程序文件转成对应的编码格式。一般情况下是按照utf8读取的。 这个编码问题有人发起PR了,可以等待后续版本修复

0
  ServletOutputStream outputStream = response.getOutputStream();
  outputStream.write(0xef);
  outputStream.write(0xbb);
  outputStream.write(0xbf);
  outputStream.flush();
  EasyExcel.write(response.getOutputStream(), clazz)
          .excelType(ExcelTypeEnum.CSV)
          .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
          .sheet()
          .doWrite(data);

第2,3,4行,先向csv文件中写入bom指定文件字符编码,这样excel在读取该文件时就能够知道该文件时utf8的而不是ansi

6

请问gb2312编码csv读取乱码的问题解决了吗?