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

devtools/2024/10/18 21:22:28/

读取大数据量的excel

代码如下

private static RowHandler createRowHandler() {return new RowHandler() {@Overridepublic 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名称问题

代码如下

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

将报错

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);即可解决

改完后代码如下

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


http://www.ppmy.cn/devtools/88263.html

相关文章

使用excel生成国际化多语言js文件的脚本

1、创建一个空文件夹 2、终端 cnpm install xlsx3、在文件夹创建一个index.js // 导入 Node.js 内置的 fs 模块 const fs = require(fs); // 导入 xlsx 模块,用于处理 Excel 文件 const XLSX = require(xlsx);// 读取 Excel 文件 function readExcelFile(filePath) {const …

数据结构初阶之排序(上)

排序的概念及其应用 排序的概念 排序&#xff1a;所谓排序&#xff0c;就是使⼀串记录&#xff0c;按照其中的某个或某些关键字的⼤⼩&#xff0c;递增或递减的排列起来的操作。 排序的应用 如下图&#xff1a; 样例数组 下面我们给出一组乱序的数组&#xff0c;接下来的算…

代码随想录算法训练营第三十三天 | 动态规划理论基础、509. 斐波那契数、70. 爬楼梯、746. 使用最小花费爬楼梯

一、动态规划理论基础 文章讲解&#xff1a;代码随想录 (programmercarl.com)——动态规划理论基础 视频讲解&#xff1a;从此再也不怕动态规划了&#xff0c;动态规划解题方法论大曝光 &#xff01;| 理论基础 |力扣刷题总结| 动态规划入门_哔哩哔哩_bilibili 题目分类&#x…

Windows 安装Redis7.4版本图文教程

本章教程&#xff0c;主要介绍如何在Windows上安装Redis7.4版本的Redis&#xff0c;并以服务方式实现开机自启动。 1、下载安装包 通过百度网盘分享的文件&#xff1a;Redis-7.4.0-Windows-x64-cygwin-with-Service.zip 链接&#xff1a;https://pan.baidu.com/s/1NFGXrCwumDzl…

如何强化学习神经网络

强化学习&#xff08;Reinforcement Learning, RL&#xff09;神经网络是一种通过奖励和惩罚机制来学习策略的方法&#xff0c;适用于各种复杂的决策问题。以下是强化学习神经网络的一些主要步骤和方法&#xff1a; 1. 了解基本概念 环境&#xff08;Environment&#xff09;…

MySQL 存储引擎

一&#xff0c;存储引擎概述 1&#xff1a;什么是存储引擎 存储引擎是数据库管理系统中负责数据存储和检索的部分。在关系型数据库系统中&#xff0c;存储引擎定义了数据如何被物理地存储、索引以及如何执行事务。不同的存储引擎提供不同的功能集&#xff0c;例如支持事务处理…

XMLDecoder反序列化

XMLDecoder反序列化 基础知识 就简单讲讲吧&#xff0c;就是为了解析xml内容的 一般我们的xml都是标签属性这样的写法 比如person对象以xml的形式存储在文件中 在decode反序列化方法后&#xff0c;控制台成功打印出反序列化的对象。 就是可以根据我们的标签识别是什么成分…

2024.7.24 作业

1.二叉树的创建、遍历自己实现一遍 bitree.h #ifndef BITREE_H #define BITREE_H#include <myhead.h>typedef char datatype;typedef struct Node {datatype data;struct Node *left_child;struct Node *right_child; }Node,*BiTreePtr;//创建二叉树 BiTreePtr tree_cr…