8
版本
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.2.1</version>
</dependency>
示例代码
public static void main(String[] args) {
Charset gbk = Charset.forName("gbk");
ZipInputStream zIn = new ZipInputStream(IoUtil.toStream(new File("e://fund_bill_20160405.zip")), gbk);//输入源zip路径
ZipEntry entry;
while ((entry = zIn.getNextEntry()) != null && !entry.isDirectory()) {
EasyExcel.read(zIn, new PageReadListener<Map<Integer, String>>(dataList -> {
System.out.println(dataList);
})).excelType(ExcelTypeEnum.CSV).autoCloseStream(false).charset(Charset.forName("gbk")).sheet().doRead();
System.out.println(entry.getName());
}
}
把e://fund_bill_20160405.zip替换成随便一个多个csv压缩成的zip文件,就可以直接运行
报错截图 原因分析由于ZipInputStream的特殊性,该流是可以通过getNextEntry进行多次读取的,但是autoCloseStream参数没起作用,在红框处的代码就会直接把流关闭,黄框执不执行不影响结果.
临时解决方案临时的结局方案是通过中转流来做,不直接操作原始流,但是还是希望尽快修复
引入 <dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.6</version>
</dependency>
示例代码
public static void main(String[] args) {
Charset gbk = Charset.forName("gbk");
ZipInputStream zIn = new ZipInputStream(IoUtil.toStream(new File("e://fund_bill_20160405.zip")), gbk);//输入源zip路径
ZipEntry entry;
while ((entry = zIn.getNextEntry()) != null && !entry.isDirectory()) {
ByteArrayOutputStream tempOutSteam = new ByteArrayOutputStream();
IoUtil.copy(zIn, tempOutSteam);
InputStream tempInputStream = new ByteArrayInputStream(tempOutSteam.toByteArray());
EasyExcel.read(tempInputStream, new PageReadListener<Map<Integer, String>>(dataList -> {
System.out.println(dataList);
})).excelType(ExcelTypeEnum.CSV).autoCloseStream(false).charset(Charset.forName("gbk")).sheet().doRead();
System.out.println(entry.getName());
}
}