Search in sources :

Example 11 with SharedStringsTable

use of org.apache.poi.xssf.model.SharedStringsTable in project yyl_example by Relucent.

the class XlsxParser method doParse.

private void doParse(InputStream input) throws IOException, OpenXML4JException, SAXException {
    OPCPackage pkg = OPCPackage.open(input);
    XSSFReader xssfReader = new XSSFReader(pkg);
    SharedStringsTable table = xssfReader.getSharedStringsTable();
    XlsxHandler handler = new XlsxHandler(table);
    XMLReader parser = createXMLReader(table, handler);
    Iterator<InputStream> sheets = xssfReader.getSheetsData();
    int sheetIndex = 0;
    while (sheets.hasNext()) {
        handler.reader = sheetReaders.get(sheetIndex++);
        try (InputStream sheet = sheets.next()) {
            if (handler.reader != null) {
                InputSource source = new InputSource(sheet);
                parser.parse(source);
            }
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) SharedStringsTable(org.apache.poi.xssf.model.SharedStringsTable) InputStream(java.io.InputStream) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) XMLReader(org.xml.sax.XMLReader) XSSFReader(org.apache.poi.xssf.eventusermodel.XSSFReader)

Example 12 with SharedStringsTable

use of org.apache.poi.xssf.model.SharedStringsTable in project timbuctoo by HuygensING.

the class XlsxLoader method loadData.

@Override
public void loadData(List<Tuple<String, File>> files, Importer importer) throws InvalidFileException, IOException {
    try {
        RowCellHandler rowCellHandler = makeRowCellHandler(importer);
        SAXParserFactory saxFactory = SAXParserFactory.newInstance();
        saxFactory.setNamespaceAware(true);
        for (Tuple<String, File> file : files) {
            OPCPackage pkg = OPCPackage.open(file.getRight().getPath());
            XSSFReader xssfReader = new XSSFReader(pkg);
            final SharedStringsTable sharedStringsTable = xssfReader.getSharedStringsTable();
            XSSFReader.SheetIterator worksheets = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
            while (worksheets.hasNext()) {
                final InputStream sheet = worksheets.next();
                XMLReader sheetParser = saxFactory.newSAXParser().getXMLReader();
                sheetParser.setContentHandler(new SheetXmlParser(sharedStringsTable, rowCellHandler));
                rowCellHandler.start(worksheets.getSheetName());
                sheetParser.parse(new InputSource(sheet));
                rowCellHandler.finish();
            }
        }
    } catch (SAXException | OpenXML4JException | ParserConfigurationException e) {
        throw new InvalidFileException("Not a valid Excel file", e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream) SAXException(org.xml.sax.SAXException) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) SharedStringsTable(org.apache.poi.xssf.model.SharedStringsTable) InvalidFileException(nl.knaw.huygens.timbuctoo.bulkupload.InvalidFileException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) XSSFReader(org.apache.poi.xssf.eventusermodel.XSSFReader)

Example 13 with SharedStringsTable

use of org.apache.poi.xssf.model.SharedStringsTable in project pentaho-kettle by pentaho.

the class StaxPoiSheetTest method mockSharedStringsTable.

private SharedStringsTable mockSharedStringsTable(String... strings) {
    SharedStringsTable sst = new SharedStringsTable();
    for (String str : strings) {
        CTRst st = CTRst.Factory.newInstance();
        st.setT(str);
        sst.addEntry(st);
    }
    return sst;
}
Also used : SharedStringsTable(org.apache.poi.xssf.model.SharedStringsTable) CTRst(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst)

Aggregations

SharedStringsTable (org.apache.poi.xssf.model.SharedStringsTable)13 XSSFReader (org.apache.poi.xssf.eventusermodel.XSSFReader)8 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)7 InputStream (java.io.InputStream)5 InputSource (org.xml.sax.InputSource)5 XMLReader (org.xml.sax.XMLReader)5 StylesTable (org.apache.poi.xssf.model.StylesTable)4 Test (org.junit.Test)3 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)2 Cell (org.apache.poi.ss.usermodel.Cell)2 Row (org.apache.poi.ss.usermodel.Row)2 Sheet (org.apache.poi.ss.usermodel.Sheet)2 CTRst (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst)2 OpenException (com.monitorjbl.xlsx.exceptions.OpenException)1 ReadException (com.monitorjbl.xlsx.exceptions.ReadException)1 File (java.io.File)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 HashMap (java.util.HashMap)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1