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();
            }
        }
    }

}
相关推荐
CodeCraft Studio4 小时前
Excel处理控件Aspose.Cells教程:使用 Python 在 Excel 中进行数据验
开发语言·python·excel
时间之城5 小时前
笔记:记一次使用EasyExcel重写convertToExcelData方法无法读取@ExcelDictFormat注解的问题(已解决)
java·spring boot·笔记·spring·excel
VBAMatrix5 小时前
审计效率升级!快速匹配Excel报表项目对应的Word附注序号
excel·审计·财务报表·会计师事务所·审计工具
lovely_nn7 小时前
wps excel 常用操作
excel·wps
前端极客探险家10 小时前
前端 Excel 工具组件实战:导入 → 可编辑表格 → 导出 + 样式同步 + 单元格合并
前端·typescript·vue·excel
AAA顶置摸鱼11 小时前
使用 Pandas 进行多格式数据整合:从 Excel、JSON 到 HTML 的处理实战
json·excel·pandas
神奇侠20242 天前
基于PaddleOCR对图片中的excel进行识别并转换成word(一)
python·word·excel·paddleocr
林枫依依2 天前
Unity 将Excel表格中的数据导入到Mysql数据表中
数据库·mysql·excel
CodeJourney.2 天前
基于DeepSeek与Excel的动态图表构建:技术融合与实践应用
数据库·人工智能·算法·excel
神奇侠20242 天前
基于PaddleOCR对图片中的excel进行识别并转换成word优化(二)
opencv·excel·paddleocr