POI XSSF - SAX(事件API)

时间:2015-01-19 18:54:38

标签: java excel apache-poi sax xssf

我想知道我是如何做一个循环并从xlsx获取单元格值,但是我使用sax事件api因为文件太大了。

我试过了:

for (Row r : sheet) {
    circuito.setConjuntoCircuito(r.getCell(1).getStringCellValue());
}

但是无法获得价值。

这是我加载文件的代码:

    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 {
    ProcessaExcel howto = new ProcessaExcel();
    howto.processAllSheets("C:\\importacaoGSPODA\\OCORRENCIAS\\x_OcorrenciasJaneiro.xlsx");
}

抱歉,我的英语很糟糕,希望有人可以提供帮助。 感谢。

0 个答案:

没有答案