使用(POI)SAX处理Excel大文件,防止内存溢出
POISAXReader
h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}
a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}
h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}
/* LINKS
=============================================================================*/
a {
color: #4183C4;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
/* CODE
=============================================================================*/
pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}
code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}
pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}
pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}
pre code, pre tt {
background-color: transparent;
border: none;
}
kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}
/* QUOTES
=============================================================================*/
blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}
blockquote>:first-child {
margin-top: 0px;
}
blockquote>:last-child {
margin-bottom: 0px;
}
/* HORIZONTAL RULES
=============================================================================*/
hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}
/* IMAGES
=============================================================================*/
img {
max-width: 100%
}
-->
解决POI读取XLSX文件内存占用过过多
poi处理excel分别提供比较友好的用户模式以及比较底层的事件模式。其中,用户模式提供良好的封装,同时兼容2003以及2007以上的格式,使用相当方便。不过,代价是花费巨大的内存。只要超过6w条以后,基本是就是内存溢出了。
好在POI团队也提供了更底层的的流处理模式eventMode,对于大数据的Xlsx文件的写入,poi 3.8 提供SXSSF,采用缓存方式写如文件。对于文件的读取采用sax的方式直接读取每个sheet对应的xml文件。添加微信,回复poi,邀请你加群
POI SheetContentsHandler 接口
在POI中已经对SAX当时读取对应的Sheet的xml文件已经做了基本的封装,所以我们仅仅需要实现接口SheetContentsHandler,就可以完成SAX的方式读取。这个接口中需要是实现三个方法
- public void startRow(int rowNum) 读取某行开始
- public void endRow(int rowNum) 读取某行结束
- public void cell(String cellReference, String formattedValue,XSSFComment comment) 读取某行中的单元格
- public void headerFooter(String text, boolean isHeader, String tagName) 暂时不清楚
POI SheetContentsHandler实现
这里我主要参照poi XLSX2CSV.java实现方式,需要提供对应的xlsx文件最大列数。其次,我在此基础上做了扩展,在 endRow 提供了一个事件,当前处理的的行数据,让这个解析功能更加独立。
实现思路,在startRow方法中构造一个List对象,在cell函数中添加每个单元内容,在endRow函数中判断当前列是否等于最大列数,如果不等循环补齐,并出发添加行事件
SheetSaxHandler详细代码
protected class SheetSaxHandler implements SheetContentsHandler {
private int currentRow = -1;
private int currentCol = -1;
private int minColumns;
public void setMinColumns(int minColumns) {
this.minColumns = minColumns;
}
public SheetSaxHandler(int minColumns) {
super();
this.minColumns = minColumns;
}
public SheetSaxHandler() {
}
private List<SheetRowListener> listeners = new ArrayList<SheetRowListener>();
private List<String> lRows = new ArrayList<String>(); // 处理一行信息
public void rowAdded(SheetRowListener add) {
listeners.add(add);
}
private void postRowAdded(List<String> row, int rowNum)
throws SQLException {
for (SheetRowListener hl : listeners)
hl.addRow(row, rowNum);
}
@Override
public void startRow(int rowNum) {
currentRow = rowNum;
currentCol = -1;
lRows.clear();
}
@Override
public void endRow(int rowNum) {
// 添加数据
for (int i = currentCol; i < minColumns; i++) {
lRows.add("");
}
try {
postRowAdded(lRows, rowNum);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void cell(String cellReference, String formattedValue,
XSSFComment comment) {
if (cellReference == null) {
cellReference = new CellAddress(currentRow, currentCol)
.formatAsString();
}
int thisCol = (new CellReference(cellReference)).getCol();
int missedCols = thisCol - currentCol - 1;//处理数据中间存在空白
for (int i = 0; i < missedCols; i++) {
this.lRows.add("");
}
currentCol = thisCol;
// TODO 数据类型处理
try {
Double.parseDouble(formattedValue);
this.lRows.add(formattedValue);
} catch (NumberFormatException e) {
this.lRows.add(formattedValue);
}
}
@Override
public void headerFooter(String text, boolean isHeader, String tagName) {
System.out.println(text + "==" + isHeader + "==" + tagName);
}
}
事件接口
interface SheetRowListener {
void addRow(List<String> row, int rowNum);
}
调用方式
- 打开文件
- 找到对应sheet的xml文件
- 使用上边的方法依次处理每一个sheet
处理文件
@Override
public int saveToOracle(String filePath, String pcId)
throws FileNotFoundException, EncryptedDocumentException,
InvalidFormatException, IOException, ClassNotFoundException,
SQLException, OpenXML4JException, SAXException,
ParserConfigurationException {
File f = new File(filePath);
OPCPackage p = null;
int num = 0;
Connection conn = null;
if (f.exists()) {
try {
JSONArray sheetCfgs = this.cfgJson.getJSONArray("sheets");
dataBuferRows = this.cfgJson.getInteger("dataBuferRows");
dataBuferRows = dataBuferRows == null ? 1000 : dataBuferRows;
conn = ca.getConnection(ca.getSqlCfg(serverPath));
String importTime = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
p = OPCPackage.open(f, PackageAccess.READ);
ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(
p);
XSSFReader xssfReader = new XSSFReader(p);
StylesTable styles = xssfReader.getStylesTable();
XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader
.getSheetsData();
HashMap<Integer, JSONObject> hSheetCfg = new HashMap<Integer, JSONObject>();
for (int i = 0; i < sheetCfgs.size(); i++) {
JSONObject sheetCfg = sheetCfgs.getJSONObject(i);
hSheetCfg.put(sheetCfg.getInteger("sheetIndex"), sheetCfg);
}
int index = 1;
while (iter.hasNext()) {
InputStream sheetStream = iter.next();
if (hSheetCfg.containsKey(index)) {
processSheet(styles, strings, new SheetSaxHandler(),
sheetStream, hSheetCfg.get(index), conn, pcId,
this.fileName, importTime);
}
index++;
}
p.close();
f = null;
conn.close();
} catch (SQLException e) {
conn.close();
conn = null;
throw e;
}
}
return num;
}
处理Sheet
public void processSheet(StylesTable styles,
ReadOnlySharedStringsTable strings, SheetSaxHandler sheetHandler,
InputStream sheetInputStream, final JSONObject sheetCfg,
final Connection conn, String PcID, String fileName,
String importTime) throws IOException,
ParserConfigurationException, SAXException, SQLException {
final PreparedStatement ps = conn.prepareStatement(ca.buildInsertSql(
sheetCfg, PcID, fileName, importTime));
final int dataStartNum = sheetCfg.getIntValue("dataStartNum");
sheetHandler.setMinColumns(sheetCfg.getJSONArray("fieldReference")
.size());
sheetHandler.rowAdded(new SheetRowListener() {
@Override
public void addRow(List<String> row, int rowNum) {
if (rowNum < dataStartNum - 1)
return;
try {
ca.setParamter(ps, sheetCfg, row, rowNum - dataStartNum);
if (rowNum % dataBuferRows == 0) {
ps.executeBatch();
ps.clearBatch();
}
} catch (SQLException e) {
try {
ps.close();
conn.close();
throw e;
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
}
});
XMLReader sheetParser = SAXHelper.newXMLReader();
DataFormatter formatter = new DataFormatter();
InputSource sheetSource = new InputSource(sheetInputStream);
ContentHandler handler = new XSSFSheetXMLHandler(styles, null, strings,
sheetHandler, formatter, false);
sheetParser.setContentHandler(handler);
sheetParser.parse(sheetSource);
// 处理剩下的数据
ps.executeBatch();
ps.clearBatch();
// 关闭当前ps
ps.close();
}
`
总结
在最初使用poi的用户模式,很快的就完成一个excel文件的解析,很方便。随着项目的逐渐深入,处理的excel文件越来越大,用户模式已经不能胜任。于是开始查找资料,在官网上看到了转csv的实例。
这段代码的主要功能将excel文件中的数据导入到oracle数据库对应的表中,在实现功能方面,我主要遇到了以下问题
- 解决excel文件解析内存泄露(2007以后文件采用sax方式基本解决)
- 对应大量数据的保存,速度一直很慢,尽管我这里采用了批量提交的方式(目前这问题我依然没找到很好的方案,如果有同行看到的,还希望多多指教)
使用(POI)SAX处理Excel大文件,防止内存溢出的更多相关文章
- 图片_ _Android有效解决加载大图片时内存溢出的问题 2
Android有效解决加载大图片时内存溢出的问题 博客分类: Android Android游戏虚拟机算法JNI 尽量不要使用setImageBitmap或 setImageResource或 Bit ...
- Android开发中如何解决加载大图片时内存溢出的问题
Android开发中如何解决加载大图片时内存溢出的问题 在Android开发过程中,我们经常会遇到加载的图片过大导致内存溢出的问题,其实类似这样的问题已经屡见不鲜了,下面将一些好的解决方案分享给 ...
- 使用(POI)SAX处理Excel文件,防止内存溢出
POISAXReader h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-chi ...
- POI Sax 事件驱动解析Excel2007文件
Excel2007版本的代码如下,本文主要是用于POI解析大文件Excel容易出现内存溢出的现象而提出解决方案,故此解决了大数据量的Excel文件解析的难度,在此拿出来贡献给大家,谢谢! 里面用到的相 ...
- POI Sax 事件驱动解析Excel2003文件
POI事件驱动解析Excel文件 package com.boguan.bte.util.excel; import java.io.FileInputStream; import java.io.I ...
- c++ windows下读取大文件(内存映射)
关于内存映射的基本知识以及一些函数的原型说明,参考博客:http://blog.csdn.net/wcyoot/article/details/7363393 下面是我对于读取一个104M文件大小,使 ...
- php导出为excel文件避免内存溢出
轻松解决PHPExcel导出10W行超时和内存溢出问题 使用了一个轻量级的PHP的Excel操作库-PHP_XLSXWriter 10w行excel数据导出仅需要5.26秒,再也不用担心excel ...
- java 大数据处理之内存溢出解决办法(一)
http://my.oschina.net/songhongxu/blog/209951 一.内存溢出类型 1.java.lang.OutOfMemoryError: PermGen space JV ...
- Android学习笔记_51_转android 加载大图片防止内存溢出
首先来还原一下堆内存溢出的错误.首先在SD卡上放一张照片,分辨率为(3776 X 2520),大小为3.88MB,是我自己用相机拍的一张照片.应用的布局很简单,一个Button一个ImageView, ...
随机推荐
- leetcode python快乐数
编写一个算法来判断一个数是不是“快乐数” “快乐数”的定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复该过程直到为1,也可能是无限循环但始终变不到1. 如果可以变为1,那 ...
- ***新版微信H5支付技术总结(原创)
新版微信H5支付官方文档: https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=9_20&index=1 H5支付是指商户在微信客户端外 ...
- Fisher–Yates shuffle 洗牌算法
Fisher-Yates shuffle 是一种生成有限序列的随机排列的算法--简单地说,该算法可以对序列进行混排.本人能力有限,且懒.不会扒论文去研究该算法在数学上的证明,只能抄袭网上的博客总结一遍 ...
- Java 多线程系列 CountDownLatch
CountDownLatch 一个或多个线程等待其他线程完成操作后在在执行 CountDownLatch通过一个计数器来实现,await方法阻塞直到 countDown() 调用计数器归零之后释放所有 ...
- mysql 数据库扫描行数
EXPLAIN SELECT * FROM tablename1 WHERE a1 '
- C++ this指针
成员函数不能定义 this 形参,而是由编译器隐含地定义.成员函数的函数体可以显式使用 this 指针,但不是必须这么做.如果对类成员的引用没有限定,编译器会将这种引用处理成通过 this 指针的引用 ...
- 微信小程序支付遇到的坑
1,微信公众号支付和微信小程序支付有差异 微信公众号:可以直接跳转走h5的微信支付 微信小程序:在测试环境.沙箱环境使用微信公众号的跳转支付没有问题,在线上存在支付异常 最后商讨的解决方法 openi ...
- AI零基础入门之人工智能开启新时代—下篇
人工智能概述 人工智能的定义 · 人工智能是通过机器来模拟人类认识能力的一种科技能力 · 人工智能最核心的能力就是根据给定的输入做出判断或预测 · 思考:通过什么途径才能让机器具备这样的能力? · 举 ...
- Java IO 整理
1.Java IO中的操作类之间的继承关系 2.在java中使用File类表示文件本身,可以直接使用此类完成文件的各种操作,如创建.删除 3.RandomAccessFile类可以从指定位置开始读取数 ...
- Network Security final project---War Game
项目介绍: 为自己的网段设置防火墙并尝试攻击其他组 网络结构: 每组有3个机器,包含一个gateway和两个workstation,其中gateway是可以连接到其他组的gateway,但是无法连接到 ...
