当前位置:网站首页>Use the streaming mode of hutool to read large Excel files
Use the streaming mode of hutool to read large Excel files
2022-04-21 07:49:00 【LingDong Jey】
website :
In standard ExcelReader in , If the amount of data is large , Read Excel It's going to be very slow , And may cause memory overflow . So for the large amount of data Excel,Hutool Encapsulates the Sax How to read patterns .
Excel07SaxReader Support Excel2007 Format Sax Read .
Refer to the experience of others' blogs , The following tools are summarized ExcelKit: ( The complete code of the class is as follows )
package com.xxx.app.blog.common.kit;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.poi.excel.sax.Excel07SaxReader;
import cn.hutool.poi.excel.sax.handler.RowHandler;
import java.util.*;
/**
* ExcelKit excel File processing library , rely on Hutool
*/
public class ExcelKit {
private static List<Object> headLine;
private static List<Map<String, Object>> datas = new ArrayList<>();
/**
* @param pathAndName File path
* @param idOrRidOrSheetName Excel Medium sheet id perhaps rid Number or sheet name ,rid Must be added rId Prefix , for example rId1, If -1 Process all numbered sheet
*/
public static List<Map<String, Object>> readBigExcel(String pathAndName, int idOrRidOrSheetName) {
Console.log("【{}】 Start reading ... ", pathAndName);
datas.clear();
Excel07SaxReader reader = new Excel07SaxReader(new MyRowHandler());
reader.read(pathAndName, idOrRidOrSheetName);
Console.log("【{}】 Read complete ... ", pathAndName);
return datas;
}
/**
* @param pathAndName File path
* @param idOrRidOrSheetName Excel Medium sheet id perhaps rid Number or sheet name ,rid Must be added rId Prefix , for example rId1, If -1 Process all numbered sheet
* @param beanType Class types
* @param <T> The return value is List<T>
* @return
*/
public static <T> List<T> read(String pathAndName, int idOrRidOrSheetName, Class<T> beanType) {
return read(pathAndName, idOrRidOrSheetName, beanType, Collections.EMPTY_MAP);
}
/**
* @param pathAndName File path
* @param idOrRidOrSheetName Excel Medium sheet id perhaps rid Number or sheet name ,rid Must be added rId Prefix , for example rId1, If -1 Process all numbered sheet
* @param beanType Class types
* @param fieldMapping T Alias corresponding to type , That is... Corresponding to class type excel The relationship between
* @param <T> The return value is List<T>
* @return
*/
public static <T> List<T> read(String pathAndName, int idOrRidOrSheetName, Class<T> beanType, Map<String, String> fieldMapping) {
CopyOptions copyOptions = CopyOptions.create();
if (CollUtil.isNotEmpty(fieldMapping)) {
copyOptions.setFieldMapping(fieldMapping);
}
readBigExcel(pathAndName, idOrRidOrSheetName);
List<T> datalist = new ArrayList<>();
for (Map<String, Object> data : datas) {
T t = ReflectUtil.newInstanceIfPossible(beanType);
datalist.add(BeanUtil.fillBeanWithMap(data, t, copyOptions));
}
return datalist;
}
private static class MyRowHandler implements RowHandler {
@Override
public void handle(int sheetIndex, long rowIndex, List<Object> rowList) {
//Console.log("[{}] [{}] {}", sheetIndex, rowIndex, rowList);
if (rowIndex == 0) {
headLine = rowList;
} else {
Map<String, Object> tMap = new HashMap<>(rowList.size());
for (int i = 0; i < rowList.size(); i++) {
tMap.put(transStr(headLine.get(i)), rowList.get(i));
}
datas.add(tMap);
}
}
}
private static String transStr(Object obj) {
return obj == null ? "" : obj.toString();
}
}
Access usage :
// Large amount of data excel Read mode
List<Map<String,Object>> recordBMapList = null;
recordBMapList = ExcelKit.readBigExcel(" File path ",-1);
// The second parameter ,-1 Represents reading all sheet data ;0 Represents reading the first sheet data , By analogy ;
版权声明
本文为[LingDong Jey]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210630312973.html
边栏推荐
猜你喜欢
![Navicat even Oracle reports an error [no matching authentication protocol]](/img/16/4dd115fc5abc68f7d1ffa640165fd9.png)
Navicat even Oracle reports an error [no matching authentication protocol]

.net core 部署至Linux平台上访问SQL SERVER 2008 R2时提示连接超时异常

When deploying. Net core on Linux platform to access SQL Server 2008 R2, it prompts the connection timeout exception
![[Niuke brush question 18] find the longest common substring in two strings a and B](/img/eb/d86b667deca3890259fac999324cd4.png)
[Niuke brush question 18] find the longest common substring in two strings a and B

UMLet初学者使用步骤

C语言指针进阶(1.一阶与二阶指针)

Principle and introduction of elk log analysis system

【牛客刷题18】 查找两个字符串a,b中的最长公共子串

IDEA 批量修改变量名、批量替换代码快捷键

【第31题】创造两只完全相同的宠物
随机推荐
西方的二分法和东方的三分法
Oracle SQL script record
Base64 Encoding
三级菜单动态生成
GoLang项目开发基础
pip list 中已有的模块为什么没法在新project中使用,已解决
Enterprise service bus -- Introduction to muleesb
每隔30秒对局域网电脑发送一次关机命令
PLSQL Developer 14安装详解
从零开始自制实现WebServer(十五)---- 日志库部分完结啦 实用小件DOUBLE-BUFFERING优化异步写入性能
php 无限极分类(递归)
GeoServer 2.20.1 solving cross domain problems CORS
Oracle-SQL脚本记录
根据二维数组某个字段的值查找数组
可切换开始,战斗,结算三界面的飞机小对决 ,模块化,pygame,Rect,exe打包
蓝牙开源协议栈BTstack之1.0 BTstack简介
php 判断是不是同一个月
MySQL5. 7 installation and operation manual (centos7)
压缩毕业时间到20岁以前,5岁上小学上5年,提高人口数量
Bluetooth Profile Specification之(AVRCP篇)5.1AVCTP的连接和释放