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

}
相关推荐
hello_simon7 小时前
在线Excel 转换为 txt ,超方便超易用软件,在线转换,大力提升工作效率
excel
云只上14 小时前
前端界面在线excel编辑器 。node编写post接口获取文件流,使用传参替换表格内容展示、前后端一把梭。
前端·javascript·node.js·excel
Ariel_提拉米苏15 小时前
表格数据导出为Excel
前端·javascript·vue.js·excel
inxunoffice16 小时前
导入 Excel 规则批量修改 txt/html/json/xml/csv 等记事本文本文件内容
xml·excel
loong_XL17 小时前
AI excel表格分析:WPS、chatexcel
excel·wps
寒山独见君~20 小时前
【Office办公】【Excel】VLOOKUP函数-高速查找指定匹配数据,可合并2个表格
excel
inxunoffice20 小时前
根据模板将 Excel 明细数据生成 Txt 文档|邮件合并
excel
无级程序员20 小时前
基础框架系列分享:一个通用的Excel报表生成管理框架
excel·报表
浪游东戴河21 小时前
电脑基础之excel基础操作
excel·表格·对齐·电脑基础
冬天vs不冷1 天前
EasyExcel导出自动回显中文,读取自动转换码值(基于全局转换器与自定义注解)
java·excel