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

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

		<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.1.4</version>
        </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

然后导出的文件是损坏的
在这里插入图片描述

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

		<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </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方法;
最后附上导出的表格
在这里插入图片描述
内容就不展示了,字段和实体类是一一对应的

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,您可以使用Java中的ZipOutputStream类来实现压缩多个Excel文件。以下是一个简单的示例代码: ```java import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.metadata.Table; import com.alibaba.excel.support.ExcelTypeEnum; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import java.io.*; import java.util.ArrayList; import java.util.List; public class ExportExcel { public static void main(String[] args) throws IOException { // 创建多个Excel文件 List<File> fileList = createExcelFiles(); // 创建压缩包文件 File zipFile = new File("excel_files.zip"); ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(zipFile); zipOutputStream.setMethod(ZipArchiveOutputStream.DEFLATED); // 将多个Excel文件压缩到压缩包中 for (File file : fileList) { ZipArchiveEntry entry = new ZipArchiveEntry(file.getName()); entry.setSize(file.length()); zipOutputStream.putArchiveEntry(entry); FileInputStream fis = new FileInputStream(file); byte[] buffer = new byte[1024]; int len; while ((len = fis.read(buffer)) > 0) { zipOutputStream.write(buffer, 0, len); } fis.close(); zipOutputStream.closeArchiveEntry(); } // 关闭ZipOutputStream流 zipOutputStream.finish(); zipOutputStream.close(); // 删除临Excel文件 for (File file : fileList) { file.delete(); } } private static List<File> createExcelFiles() throws FileNotFoundException { List<File> fileList = new ArrayList<>(); for (int i = 1; i <= 3; i++) { // 创建Excel文件 File file = new File("excel_" + i + ".xlsx"); OutputStream out = new FileOutputStream(file); // EasyExcel导出数据 ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX); Sheet sheet = new Sheet(1, 0); sheet.setSheetName("Sheet1"); Table table = new Table(1); List<List<String>> data = new ArrayList<>(); for (int j = 1; j <= 10; j++) { List<String> row = new ArrayList<>(); row.add("Data" + j); data.add(row); } table.setData(data); writer.write0(table, sheet); writer.finish(); // 添加Excel文件到列表中 fileList.add(file); } return fileList; } } ``` 在上面的代码中,我们首先创建了3个Excel文件,然后将它们压缩到一个名为“excel\_files.zip”的压缩包中。最后,我们删除了临Excel文件。注意,我们使用了Apache Commons Compress库来实现压缩操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿韩想静静

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值