Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.0.5版本读取CSV文件,head为中文时无法读取,autoTrim无效 #2227

Closed
fufeiii opened this issue Dec 14, 2021 · 4 comments
Closed
Assignees
Labels
bug Something isn't working pending verification This problem needs to be confirmed planning It may be developed later

Comments

@fufeiii
Copy link
Contributor

fufeiii commented Dec 14, 2021

触发Bug的代码
读取一个CSV文件,第一行的标题头为中文,使用@ExcelProperty加标题名的方式读取不出数据(字段都为null),使用@ExcelProperty加index的方式可以读取,autoTrim参数无效

 EasyExcel.read(new File(localFileUrl))
 .excelType(ExcelTypeEnum.CSV)
 .head(FileHead.class)
.headRowNumber(1)
.autoTrim(true)
@fufeiii fufeiii added the bug Something isn't working label Dec 14, 2021
@fufeiii
Copy link
Contributor Author

fufeiii commented Dec 15, 2021

无法读取问题已发现原因:文件是GBK编码时出现,若是UTF8则正常读取。

@fufeiii fufeiii closed this as completed Dec 15, 2021
@fufeiii
Copy link
Contributor Author

fufeiii commented Dec 15, 2021

autoTrim问题还是存在

@fufeiii fufeiii reopened this Dec 15, 2021
@zhuangjiaju zhuangjiaju added the pending verification This problem needs to be confirmed label Feb 11, 2022
@fufeiii
Copy link
Contributor Author

fufeiii commented Mar 24, 2022

复现代码

@Test
public void test() {
    // 注意商户号列的空格
    String csvData = "序号,商户号,商户名\n" +
            "1, 商户号1 ,商户名1\n" +
            "2, 商户号2 ,商户名2\n" +
            "3, 商户号3 ,商户名3";
    EasyExcel.read(new ByteArrayInputStream(csvData.getBytes()))
            .registerReadListener(new AnalysisEventListener<RowContent>() {
                @Override
                public void invoke(RowContent o, AnalysisContext analysisContext) {
                    System.out.println(o + ",原始长度=" + o.getKey2().length() + ",trim长度后=" + o.getKey2().trim().length());
                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext context) {

                }
            })
            .autoTrim(true)
            .headRowNumber(1)
            .head(RowContent.class)
            .excelType(ExcelTypeEnum.CSV)
            .sheet()
            .doRead();
}

model类

@Data
public class RowContent {
    @ExcelProperty(value = "序号")
    private String key1;
    @ExcelProperty(value = "商户号")
    private String key2;
    @ExcelProperty(value = "商户名")
    private String key3;
}

运行结果

RowContent(key1=1, key2= 商户号1 , key3=商户名1),原始长度=6,trim长度后=4
RowContent(key1=2, key2= 商户号2 , key3=商户名2),原始长度=6,trim长度后=4
RowContent(key1=3, key2= 商户号3 , key3=商户名3),原始长度=6,trim长度后=4

@zhuangjiaju zhuangjiaju added the planning It may be developed later label Apr 6, 2022
zhuangjiaju added a commit that referenced this issue May 11, 2022
fix #2227 读取csv文件时autoTrim配置失效
@Ling280608
Copy link

文件是GBK编码时出现,若是UTF8则正常读取。

这个要怎么解决呢,我要导入的文件是对方提供的,自己不可改编码格式。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pending verification This problem needs to be confirmed planning It may be developed later
Projects
None yet
Development

No branches or pull requests

3 participants