jeecgboot / autopoi

AutoPOI 功能如同名字auto,追求的就是自动化,让一个没接触过poi的人员,可以傻瓜化的快速实现Excel导入导出、Word模板导出、可以仅仅5行代码就可以完成Excel的导入导出。
http://www.jeecg.com
Apache License 2.0
460 stars 199 forks source link

读取单元格为空字符串问题 #102

Open zkool opened 7 months ago

zkool commented 7 months ago

image

image

excel文件在这里获取的 https://udi.nmpa.gov.cn/download.html 下段代码中list里的TestEntity2的属性全是空字符串【注意:下载后的excel我用wps编辑并保存后,又没问题了。但是此处的场景是一个文件夹内好几百个excel,我不可能每个文件都编辑一遍。。。】

public class ImportTest {

    private static final String basePath = "D:\\opt\\upFiles\\temp\\UDID_DAY_20240107PART_1.xlsx";

    public static void main(String[] args) {
        File file = new File(basePath);
        ImportParams params = new ImportParams();
        params.setHeadRows(1);
        params.setStartSheetIndex(0);
        params.setSheetNum(1);
        List<TestEntity2> list = ExcelImportUtil.importExcel(file, TestEntity2.class, params);
        for (TestEntity2 testEntity2 : list) {
            System.out.println(testEntity2.getName());
            System.out.println(testEntity2.getAge());
            System.out.println(testEntity2.getStatus());
        }
    }
}

public class TestEntity2 {
    @Excel(name = "主键编号", width = 15)
    private String name;
    @Excel(name = "公开的版本号", width = 15)
    private String age;
    @Excel(name = "版本的状态", width = 15)
    private String status;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}

最后断点也没定位到问题,只好在org.jeecgframework.poi.excel.imports.CellValueServer的getCellValue(String xclass, Cell cell, ExcelImportEntity entity)方法内加一层判断,不去执行setCellType()该方法。希望autopoi团队有更好的解决方案 //设置单元格类型 if(CellType.STRING != cell.getCellTypeEnum()){ cell.setCellType(CellType.STRING); }