hutool获取大数据量的excel内容及sheet名称问题

读取大数据量的excel时

代码如下

java 复制代码
private static RowHandler createRowHandler() {
        return new RowHandler() {
            @Override
            public void handle(int i, long l, List<Object> list) {
                System.out.println(i + " " + l + " " + list);
            }
        };
    }
    public static void main(String[] args) {
        File file = FileUtil.file("d:/1.xlsx");
        ExcelUtil.readBySax(file,-1,createRowHandler());

    }

报错

NoSuchMethodError:org.apache.poi.util.XMLHelper.newXMLReader()

解决办法

修改源码

复制代码
ExcelSaxUtil中readFrom方法中的xmlReader = XMLHelper.newXMLReader();

改为

复制代码
xmlReade SAXHelper.newXMLReader();

或者升级poi到5.x

读取sheet名称问题

代码如下

java 复制代码
 public static List<String> getSheetNames() throws IOException {
        OPCPackage open = null;
        try {
            File file = FileUtil.file("d:/1.xlsx");
            open = OPCPackage.open(file, PackageAccess.READ);
            XSSFReader xssfReader = new XSSFReader(open);
            SheetRidReader parse = SheetRidReader.parse(xssfReader);
            List<String> sheetNames = parse.getSheetNames();

            return sheetNames;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (OpenXML4JException e) {
            throw new RuntimeException(e);
        } finally {
            if (open!= null){
                open.close();
            }
        }
    }

这是运行没问题的

当将open = OPCPackage.open(file, PackageAccess.READ);改为文件流的形式如下

复制代码
open = OPCPackage.open(new FileInputStream(file));

将报错

java 复制代码
Zip bomb detected! The file would exceed the max. ratio of compressed file size to the size of the expanded data.
This may indicate that the file is used to inflate memory usage and thus could pose a security risk.
You can adjust this limit via ZipSecureFile.setMinInflateRatio() if you need to work with files which exceed this limit.
Uncompressed size: 233647, Raw/compressed size: 2324, ratio: 0.009947
Limits: MIN_INFLATE_RATIO: 0.010000, Entry: xl/styles.xml

包括获取文件内容时将

复制代码
ExcelUtil.readBySax(file,-1,createRowHandler());改为流的形式如下
复制代码
ExcelUtil.readBySax(new FileInputStream(file),-1,createRowHandler());
同样会报这个错

这是因为压缩率超过范围了 在执行之前添加代码

复制代码
ZipSecureFile.setMinInflateRatio(-1.0);即可解决

改完后代码如下

java 复制代码
package org.example;

import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.sax.SheetRidReader;
import cn.hutool.poi.excel.sax.handler.RowHandler;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.util.XMLHelper;
import org.apache.poi.xssf.eventusermodel.XSSFReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

public class Test5 {

    private static RowHandler createRowHandler() {
        return new RowHandler() {
            @Override
            public void handle(int i, long l, List<Object> list) {
                System.out.println(i + " " + l + " " + list);
            }
        };
    }
    public static void main(String[] args) throws FileNotFoundException {
        ZipSecureFile.setMinInflateRatio(-1.0);
        File file = FileUtil.file("d:/1.xlsx");
        ExcelUtil.readBySax(new FileInputStream(file),-1,createRowHandler());

    }
    public static List<String> getSheetNames() throws IOException {
        OPCPackage open = null;
        try {
            ZipSecureFile.setMinInflateRatio(-1.0);
            File file = FileUtil.file("d:/1.xlsx");
            open = OPCPackage.open(new FileInputStream(file));
            XSSFReader xssfReader = new XSSFReader(open);
            SheetRidReader parse = SheetRidReader.parse(xssfReader);
            List<String> sheetNames = parse.getSheetNames();
            return sheetNames;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (OpenXML4JException e) {
            throw new RuntimeException(e);
        } finally {
            if (open!= null){
                open.close();
            }
        }
    }

}
相关推荐
图片转成excel表格1 小时前
wps怎么算出一行1和0两种数值中连续数值1的个数,出现0后不再计算?
excel·wps
q2498596931 小时前
前端预览word、excel、ppt
前端·word·excel
丁德双16 小时前
winform 加载 office excel 插入QRCode图片如何设定位置
c#·excel
IT铺子17 小时前
Excel 个人时间管理工具
excel
zxguan1 天前
EasyExcel 学习之 导出 “提示问题”
学习·excel
忘忧记1 天前
Excel批量转换不规范数据的教程
excel
神奇夜光杯1 天前
Python酷库之旅-第三方库Pandas(200)
开发语言·人工智能·python·excel·pandas·标准库及第三方库·学习与成长
火星技术1 天前
Excel快速转换文档word工具
word·excel
 嘘 1 天前
文件操作:Xml转Excel
xml·python·excel
Mr。轩。1 天前
cn.afterturn.easypoi.exception.excel.ExcelExportException: Excel导出错误 -> 修正过程。
java·excel·导出