hutool获取大数据量的excel内容及sheet名称问题
创始人
2024-11-14 16:06:03
0次

读取大数据量的excel时

代码如下

private static RowHandler createRowHandler() {         return new RowHandler() {             @Override             public void handle(int i, long l, List 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 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 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() {             @Override             public void handle(int i, long l, List 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 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 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();             }         }     }  }  

相关内容

热门资讯

每日必看教程!微乐小程序黑科技... 每日必看教程!微乐小程序黑科技免费(辅助器),微信小程序怎么开挂(有辅助挂教学)1、让任何用户在无需...
今日公布!微乐小程序免费黑科技... 今日公布!微乐小程序免费黑科技(透视器),微信小程序微乐辅助器教程图片(有辅助挂辅教程)1、完成微乐...
安装程序教程!微乐小程序辅助工... 安装程序教程!微乐小程序辅助工具(透视器),微乐小程序黑科技辅助工具2026最新推荐(证实有辅助挂)...
我来教大家!微乐小程序黑科技免... 我来教大家!微乐小程序黑科技免费(辅助),微信小程序微乐安徽辅助苹果(确实有辅助挂)1)微乐小程序黑...
热点推荐!微乐小程序黑科技免费... 热点推荐!微乐小程序黑科技免费(辅助),微信小程序透视微乐(竟然有辅助挂)1、微乐小程序黑科技免费破...
揭秘几款!微乐小程序免费黑科技... 揭秘几款!微乐小程序免费黑科技(辅助),微信微乐陕西小程序破解器(有辅助挂辅助)揭秘几款!微乐小程序...
重大通报!微乐小程序免费黑科技... 重大通报!微乐小程序免费黑科技(辅助),微乐游戏辅助脚本安装(有辅助挂辅助)1、每一步都需要思考,不...
玩家交流!微乐小程序免费黑科技... 玩家交流!微乐小程序免费黑科技(作弊器),微乐家乡官方app下载(有辅助挂辅教程)1)微乐小程序免费...
教程攻略!微乐小程序黑科技免费... 教程攻略!微乐小程序黑科技免费(开挂),微乐小程序游戏破解器下载(有辅助挂方式)1、不需要AI权限,...
终于清楚!微乐小程序黑科技免费... 终于清楚!微乐小程序黑科技免费(作弊器),微乐小程序辅助教程(有辅助挂秘籍)1、用户打开应用后不用登...