使用EasyExcel导出Excel时报错 Can not close IO 及EasyExcel.write()方法找不到,已解决

1.在使用EasyExcel导出Excel时遇到了两个问题,第一个就是网上用的很多的write()方法我写了报红,这个很简单是easyexcel的pom版本太低了,于是换了下面这个版本

		<dependency>
            <groupId>com.alibabagroupId>
            <artifactId>easyexcelartifactId>
            <version>2.1.4version>
        dependency>

2.再次导出时报错

com.alibaba.excel.exception.ExcelGenerateException: 
Can not close IO.
...
Caused by: java.lang.IncompatibleClassChangeError: 
Found interface org.apache.poi.util.POILogger, 
but class was expected

然后导出的文件是损坏的
使用EasyExcel导出Excel时报错 Can not close IO 及EasyExcel.write()方法找不到,已解决_第1张图片

原因是POI版本冲突,别的博主说用3.17以上的,但我发现自己是4.1.0满足,于是换了3.17

		<dependency>
            <groupId>org.apache.poigroupId>
            <artifactId>poiartifactId>
            <version>3.17version>
        dependency>

导出成功

在这里提供一份导出Excel的代码

导出类

@Test
    public void CrawlEntertainmentQuestions() {
        List<CrawlQuestion> crawlQuestionList = new ArrayList<>();
        //...Excel模型实体列表的数据填充,可以使数据库查的,可以使网页爬取的
        String filePath = "E:\\crawl.xlsx";
        EasyExcel.write(filePath, CrawlQuestion.class).sheet("Sheet1").doWrite(crawlQuestionList);
    }

实体类

@Data
public class CrawlQuestion {
    @ExcelProperty(value = "title",index = 1)
    private String title;
    @ExcelProperty(value = "optionA",index = 2)
    private String optionA;
    @ExcelProperty(value = "是否是答案",index = 3)
    private String isOptionA;
    @ExcelProperty(value = "optionB",index = 4)
    private String optionB;
    @ExcelProperty(value = "是否是答案",index = 5)
    private String isOptionB;
    @ExcelProperty(value = "optionC",index = 6)
    private String optionC;
    @ExcelProperty(value = "是否是答案",index = 7)
    private String isOptionC;
    @ExcelProperty(value = "optionD",index = 8)
    private String optionD;
    @ExcelProperty(value = "是否是答案",index = 9)
    private String isOptionD;
    @ExcelProperty(value = "answer",index = 10)
    private String answer;
}

@ExcelProperty这个注解是用来标注字段与Excel里的映射关系的,最左侧我空了一行,所以从index1开始;然后用到了lombok的@Data注解,作用是省略set,get方法;
最后附上导出的表格
使用EasyExcel导出Excel时报错 Can not close IO 及EasyExcel.write()方法找不到,已解决_第2张图片
内容就不展示了,字段和实体类是一一对应的

你可能感兴趣的:(POI,poi,excel,java)