poi的sax模式读取xls_POI SAX 如何修改大excel 文件内容-问答-阿里云开发者社区-阿里云...
I think POI is using too much memory! What can I do?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 thememory footprint relatively small, so this should be
able to read enormous workbooks. The styles table and
the shared-string table must be kept in memory. The
standard POI styles table class is used, but a custom
(read-only) class is used for the shared string table
because the standard POI SharedStringsTable grows
very quickly with the number of unique strings.
大概的意思就是說通用的處理方法會快速消耗掉內存,所以采用了 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文件,盡量不占用內存資源,從而達到讀取大文件的目的。
總結
以上是生活随笔為你收集整理的poi的sax模式读取xls_POI SAX 如何修改大excel 文件内容-问答-阿里云开发者社区-阿里云...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓手机软件开发_无代码手机app软件开
- 下一篇: 微信小程序原生 地区选择器