如何使用Apache POI Event API读取特定行?

时间:2012-04-25 10:20:35

标签: apache-poi

我想阅读大型xls或xlsx文件(大约超过30 MB并且有70,000多行)。我能够使用Apache POI eaily读取小的excel文件,直到我收到OutOfMemory错误。

性能和内存使用对我来说是个问题。我读了许多帖子,如果内存占用是个问题,那么对于XSSF,您可以获取基础XML数据,并使用XSSF和SAX(事件API)自行处理。好吧,我发现它很有趣,现在可以读取整个xlsx文件,没有任何问题。与几乎以GB相比,它消耗的内存要少得多(少于70 MB)(如果我将-Xmx设置为1024m并且仍然用于挂起,则会高达1GB),而不使用事件API。

但是现在我想自定义读取过程并且只允许从excel读取特定行。我可以使用org.apache.poi.ss.usermodel.Sheet #getRow(int rownum)轻松完成此操作。但是使用Event API它会读取所有行而没有任何中断,我发现很难读取特定的行,例如只是行号2,3,5等。下面是我的整个代码:

import java.io.InputStream;
import java.util.Iterator;
import java.util.Vector;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 * XSSF and SAX (Event API)
 */
public class FromHowTo {
    public void processAllSheets(String filename) throws Exception {
        OPCPackage pkg = OPCPackage.open(filename);
        XSSFReader r = new XSSFReader( pkg );
        SharedStringsTable sst = r.getSharedStringsTable();

        XMLReader parser = fetchSheetParser(sst);

        Iterator<InputStream> sheets = r.getSheetsData();
        while(sheets.hasNext()) {
            InputStream sheet = sheets.next();
            InputSource sheetSource = new InputSource(sheet);
            parser.parse(sheetSource);
            sheet.close();
        }
    }

    public XMLReader fetchSheetParser(SharedStringsTable sst) throws SAXException {
        XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
        ContentHandler handler = new SheetHandler(sst);
        parser.setContentHandler(handler);
        return parser;
    }

    /** 
     * See org.xml.sax.helpers.DefaultHandler javadocs 
     */
    private static class SheetHandler extends DefaultHandler {
        private SharedStringsTable sst;
        private String lastContents;
        private boolean nextIsString;
        Vector values = new Vector(10);

        private SheetHandler(SharedStringsTable sst) {
            this.sst = sst;
        }

        public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
            // c => cell

            if(name.equals("c")) {
                // Figure out if the value is an index in the SST
                String cellType = attributes.getValue("t");
                //System.out.println(cellType);
                if(cellType != null && cellType.equals("s")) {
                    nextIsString = true;
                } else {
                    nextIsString = false;
                }
            }
            // Clear contents cache
            lastContents = "";
        }

        public void endElement(String uri, String localName, String name) throws SAXException {
            // Process the last contents as required.
            // Do now, as characters() may be called more than once
            if(nextIsString) {
                try {
                    int idx = Integer.parseInt(lastContents);
                    lastContents = new XSSFRichTextString(sst.getEntryAt(idx)).toString();
                } catch (NumberFormatException e) {
                }
            }

            // v => contents of a cell
            // Output after we've seen the string contents
            if(name.equals("v")) {
                values.add(lastContents);
            }

            if(name.equals("row")) {
                System.out.println(values);
                values.removeAllElements();
            }
        }

        public void characters(char[] ch, int start, int length) throws SAXException {
            lastContents += new String(ch, start, length);
        }
    }

    public static void main(String[] args) throws Exception {
        FromHowTo howto = new FromHowTo();
        howto.processAllSheets(args[0]);
    }
}

我正在使用JRE7和Apache POI 3.7。有人可以帮我用Event API获取特定的行吗?

1 个答案:

答案 0 :(得分:5)

每行start元素都有一个行号。它可以从属性中检索

long rowIndex = Long.valueOf(attributes.getValue(“r”));

事件模型将遍历所有行,但您可以在endElement中相应地索引和处理数据