我现在要解析一个大的excel文件,直接用POI的方式会OOM,现在用POI SAX(事件驱动模式)问题解决了。
但是现在处理完数据需要每行返回一个状态,添加到行末尾,其他数据格式保持原样。这个请问怎么处理?
首先先去http://poi.apache.org/faq.html :这个是个常用的问题列表,在
This one comes up quite a lot, but often the reason isn't what you might initially think. So, the first thing to check is - what's the source of the problem? Your file? Your code? Your environment? Or Apache POI?
Next, use the example program ToCSV to try reading the a file in with HSSF or XSSF. Related is XLSX2CSV, which uses SAX parsing for .xlsx
我们看到了POI已经提出了针对这个问题的回答,而且正好在XLS2CSV中找到了很好的解决读取大容量的方法.
在XLS2CSV中,即使是读取300M的2007excel文件也没有什么问题,占用的内存也是可以接受的。
在这段代码中的注释中写出了下面一段话:
*Data sheets are read using a SAX parser to keep the
大概的意思就是说通用的处理方法会快速消耗掉内存,所以采用了 SAX parser方法来处理大容量的文件.
public void processSheet(
StylesTable styles,
ReadOnlySharedStringsTable strings,
InputStream sheetInputStream)
throws IOException, ParserConfigurationException, SAXException {
InputSource sheetSource = new InputSource(sheetInputStream);
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
SAXParser saxParser = saxFactory.newSAXParser();
XMLReader sheetParser = saxParser.getXMLReader();
ContentHandler handler = new MyXSSFSheetHandler(styles, strings, this.minColumns, this.output);
sheetParser.setContentHandler(handler);
sheetParser.parse(sheetSource);
}
上面的一段代码是比较核心的代码,通过SAXParser解析xml文件,尽量不占用内存资源,从而达到读取大文件的目的。
XLSX2CSV代码下载: http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/eventusermodel/XLSX2CSV.java
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。