Example usage for org.apache.poi.xssf.eventusermodel XSSFSheetXMLHandler XSSFSheetXMLHandler

List of usage examples for org.apache.poi.xssf.eventusermodel XSSFSheetXMLHandler XSSFSheetXMLHandler

Introduction

In this page you can find the example usage for org.apache.poi.xssf.eventusermodel XSSFSheetXMLHandler XSSFSheetXMLHandler.

Prototype

public XSSFSheetXMLHandler(Styles styles, SharedStrings strings, SheetContentsHandler sheetContentsHandler,
        boolean formulasNotResults) 

Source Link

Document

Accepts objects needed while parsing.

Usage

From source file:com.myjeeva.poi.ExcelReader.java

License:Open Source License

/**
 * Parses the content of one sheet using the specified styles and shared-strings tables.
 * /*  w w w.  j  a  v  a 2s  .co  m*/
 * @param styles a {@link StylesTable} object
 * @param sharedStringsTable a {@link ReadOnlySharedStringsTable} object
 * @param sheetInputStream a {@link InputStream} object
 * @throws IOException
 * @throws ParserConfigurationException
 * @throws SAXException
 */
private void readSheet(StylesTable styles, ReadOnlySharedStringsTable sharedStringsTable,
        InputStream sheetInputStream) throws IOException, ParserConfigurationException, SAXException {

    SAXParserFactory saxFactory = SAXParserFactory.newInstance();
    XMLReader sheetParser = saxFactory.newSAXParser().getXMLReader();

    ContentHandler handler = new XSSFSheetXMLHandler(styles, sharedStringsTable, sheetContentsHandler, true);

    sheetParser.setContentHandler(handler);
    sheetParser.parse(new InputSource(sheetInputStream));
}

From source file:com.myjeeva.poi.ExcelXSSFRowCallbackHandler.java

License:Apache License

/**
 * Parses the file, passing each row to the given callback.
 * At the end closes the opc package and underling input stream.
 *///from ww w .  j a v  a  2 s. c  om
public void parse() throws Exception {

    XSSFReader reader = new XSSFReader(this.opcPackage);

    StylesTable styles = reader.getStylesTable();
    ReadOnlySharedStringsTable sharedStrings = new ReadOnlySharedStringsTable(this.opcPackage);

    ContentHandler handler = new XSSFSheetXMLHandler(styles, sharedStrings,
            new ExcelXSSFRowCallbackSheetContentsHandler(this.rowCallback), true);

    XMLReader parser = XMLReaderFactory.createXMLReader();
    parser.setContentHandler(handler);

    InputStream sheetInputStream = reader.getSheetsData().next();

    try {

        parser.parse(new InputSource(sheetInputStream));

    } finally {

        IOUtils.closeQuietly(sheetInputStream);
        this.opcPackage.close();

    }

}

From source file:com.opendoorlogistics.core.tables.io.XmlParserLoader.java

License:Open Source License

private void parseSheet(StylesTable styles, ReadOnlySharedStringsTable sst, InputSource sheetSource,
        SheetContentsHandler handler) {/*ww  w  .ja  v a  2s.  c  om*/
    try {
        createSheetParser(new XSSFSheetXMLHandler(styles, sst, handler, false)).parse(sheetSource);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.rcv.StreamingCVRReader.java

License:Open Source License

List<CastVoteRecord> parseCVRFile(List<CastVoteRecord> castVoteRecords, Set<String> precinctIDs)
        throws UnrecognizedCandidatesException, OpenXML4JException, SAXException, IOException {

    // cache the cvr list so it is accessible in callbacks
    cvrList = castVoteRecords;/*from ww w .j  a  v a 2s . co  m*/
    // cache precinctIDs set so it is accessible in callbacks
    this.precinctIDs = precinctIDs;

    // open the zip package
    OPCPackage pkg = OPCPackage.open(excelFilePath);
    // pull out strings
    ReadOnlySharedStringsTable sharedStrings = new ReadOnlySharedStringsTable(pkg);
    // XSSF reader is used to extract styles data
    XSSFReader xssfReader = new XSSFReader(pkg);
    // styles data is used for creating ContentHandler
    StylesTable styles = xssfReader.getStylesTable();
    // SheetContentsHandler is used to handle parsing callbacks
    SheetContentsHandler sheetContentsHandler = new SheetContentsHandler() {
        // function: startRow
        // purpose: startRow callback handler during xml parsing
        // param: i the row which has started
        @Override
        public void startRow(int i) {
            if (i >= firstVoteRowIndex) {
                beginCVR();
            }
        }

        // function: endRow
        // purpose: endRow callback handler during xml parsing
        // row has completed, we will create a new cvr object
        // param: i the row which has ended
        @Override
        public void endRow(int i) {
            if (i >= firstVoteRowIndex) {
                endCVR();
            }
        }

        // function: cell
        // purpose: cell callback handler during xml parsing
        // param: s cell address encoded as col,row
        // param: s1 cell data
        // param: xssfComment additional cell data (unused)
        @Override
        public void cell(String s, String s1, XSSFComment xssfComment) {
            // address contains the row and col of this cell
            Pair<Integer, Integer> address = getCellAddress(s);
            int col = address.getKey();
            int row = address.getValue();
            if (row >= firstVoteRowIndex) {
                cvrCell(col, s1);
            }
        }

        // function: headerFooter
        // purpose: header footer callback from xml parsing - unused
        // param: s header footer data
        // param: b header footer data
        // param: s1 header footer data
        @Override
        public void headerFooter(String s, boolean b, String s1) {
            Logger.log(Level.WARNING, String.format("Unexpected XML data: %s %b %s", s, b, s1));
        }
    };

    // create the ContentHandler to handle parsing callbacks
    ContentHandler handler = new XSSFSheetXMLHandler(styles, sharedStrings, sheetContentsHandler, true);

    // create the XML reader and set content handler
    XMLReader xmlReader = XMLReaderFactory.createXMLReader();
    xmlReader.setContentHandler(handler);
    // trigger parsing
    xmlReader.parse(new InputSource(xssfReader.getSheetsData().next()));

    // throw if there were any unrecognized candidates -- this is considered bad
    if (this.unrecognizedCandidateCounts.size() > 0) {
        throw new UnrecognizedCandidatesException(unrecognizedCandidateCounts);
    }

    // return the input list with additions
    return cvrList;
}