List of usage examples for org.apache.poi.xssf.eventusermodel XSSFReader XSSFReader
public XSSFReader(OPCPackage pkg) throws IOException, OpenXML4JException
From source file:net.geoprism.data.etl.excel.ExcelSheetReader.java
License:Open Source License
public void process(InputStream stream) throws Exception { try {//from w ww. jav a 2s . com OPCPackage pkg = OPCPackage.open(stream); ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(pkg); XSSFReader xssfReader = new XSSFReader(pkg); StylesTable styles = xssfReader.getStylesTable(); XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData(); while (iter.hasNext()) { InputStream sheet = iter.next(); try { String sheetName = iter.getSheetName(); this.handler.startSheet(sheetName); InputSource sheetSource = new InputSource(sheet); ContentHandler handler = new XSSFSheetXMLHandler(styles, strings, this.handler, this.formatter, false); XMLReader reader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser"); reader.setContentHandler(handler); reader.parse(sheetSource); this.handler.endSheet(); } finally { sheet.close(); } } } finally { stream.close(); } }
From source file:oracle.cloud.sampleapps.Excel2Csv.XLSX2CSV.java
License:Apache License
/** * Initiates the processing of the XLS workbook file to CSV. * * @throws IOException//from www .ja va 2 s . c om * @throws OpenXML4JException * @throws ParserConfigurationException * @throws SAXException */ public String process() throws IOException, OpenXML4JException, ParserConfigurationException, SAXException { ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(this.xlsxPackage); XSSFReader xssfReader = new XSSFReader(this.xlsxPackage); StylesTable styles = xssfReader.getStylesTable(); XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData(); int index = 0; while (iter.hasNext()) { InputStream stream = iter.next(); String sheetName = iter.getSheetName(); processSheet(styles, strings, new SheetToCSV(), stream); stream.close(); ++index; } return csvOutput.toString(); }
From source file:org.apache.metamodel.excel.XlsxSpreadsheetReaderDelegate.java
License:Apache License
@Override public DataSet executeQuery(Table table, Column[] columns, int maxRows) throws Exception { final OPCPackage pkg = openOPCPackage(); final XSSFReader xssfReader = new XSSFReader(pkg); final String relationshipId = _tableNamesToInternalIds.get(table.getName()); if (relationshipId == null) { throw new IllegalStateException("No internal relationshipId found for table: " + table); }/*from w w w. ja v a 2 s . co m*/ return buildDataSet(columns, maxRows, relationshipId, xssfReader, pkg); }
From source file:org.apache.metamodel.excel.XlsxSpreadsheetReaderDelegate.java
License:Apache License
@Override public Schema createSchema(String schemaName) throws Exception { final MutableSchema schema = new MutableSchema(schemaName); final OPCPackage pkg = openOPCPackage(); try {//from w ww . jav a 2 s . co m final XSSFReader xssfReader = new XSSFReader(pkg); final XlsxWorkbookToTablesHandler workbookToTables = new XlsxWorkbookToTablesHandler(schema, _tableNamesToInternalIds); buildTables(xssfReader, workbookToTables); for (Entry<String, String> entry : _tableNamesToInternalIds.entrySet()) { final String tableName = entry.getKey(); final String relationshipId = entry.getValue(); final MutableTable table = (MutableTable) schema.getTableByName(tableName); buildColumns(table, relationshipId, xssfReader); } } finally { pkg.revert(); } return schema; }
From source file:org.apache.metamodel.excel.XlsxSpreadsheetReaderDelegate.java
License:Apache License
@Override public void notifyTablesModified() { final XlsxWorkbookToTablesHandler workbookToTables = new XlsxWorkbookToTablesHandler(null, _tableNamesToInternalIds);/* ww w .j a v a 2 s .c o m*/ try { final OPCPackage pkg = openOPCPackage(); try { final XSSFReader xssfReader = new XSSFReader(pkg); buildTables(xssfReader, workbookToTables); } finally { pkg.revert(); } } catch (Exception e) { throw new IllegalStateException(e); } }
From source file:org.apache.nifi.processors.poi.ConvertExcelToCSVProcessor.java
License:Apache License
@Override public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { final FlowFile flowFile = session.get(); if (flowFile == null) { return;//from ww w. j ava 2s.c o m } final String desiredSheetsDelimited = context.getProperty(DESIRED_SHEETS).evaluateAttributeExpressions() .getValue(); final boolean formatValues = context.getProperty(FORMAT_VALUES).asBoolean(); final CSVFormat csvFormat = CSVUtils.createCSVFormat(context); //Switch to 0 based index final int firstRow = context.getProperty(ROWS_TO_SKIP).asInteger() - 1; final String[] sColumnsToSkip = StringUtils.split(context.getProperty(COLUMNS_TO_SKIP).getValue(), ","); final List<Integer> columnsToSkip = new ArrayList<>(); if (sColumnsToSkip != null && sColumnsToSkip.length > 0) { for (String c : sColumnsToSkip) { try { //Switch to 0 based index columnsToSkip.add(Integer.parseInt(c) - 1); } catch (NumberFormatException e) { throw new ProcessException("Invalid column in Columns to Skip list.", e); } } } try { session.read(flowFile, new InputStreamCallback() { @Override public void process(InputStream inputStream) throws IOException { try { OPCPackage pkg = OPCPackage.open(inputStream); XSSFReader r = new XSSFReader(pkg); ReadOnlySharedStringsTable sst = new ReadOnlySharedStringsTable(pkg); StylesTable styles = r.getStylesTable(); XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) r.getSheetsData(); if (desiredSheetsDelimited != null) { String[] desiredSheets = StringUtils.split(desiredSheetsDelimited, DESIRED_SHEETS_DELIMITER); if (desiredSheets != null) { while (iter.hasNext()) { InputStream sheet = iter.next(); String sheetName = iter.getSheetName(); for (int i = 0; i < desiredSheets.length; i++) { //If the sheetName is a desired one parse it if (sheetName.equalsIgnoreCase(desiredSheets[i])) { ExcelSheetReadConfig readConfig = new ExcelSheetReadConfig( columnsToSkip, firstRow, sheetName, formatValues, sst, styles); handleExcelSheet(session, flowFile, sheet, readConfig, csvFormat); break; } } } } else { getLogger().debug( "Excel document was parsed but no sheets with the specified desired names were found."); } } else { //Get all of the sheets in the document. while (iter.hasNext()) { InputStream sheet = iter.next(); String sheetName = iter.getSheetName(); ExcelSheetReadConfig readConfig = new ExcelSheetReadConfig(columnsToSkip, firstRow, sheetName, formatValues, sst, styles); handleExcelSheet(session, flowFile, sheet, readConfig, csvFormat); } } } catch (InvalidFormatException ife) { getLogger().error("Only .xlsx Excel 2007 OOXML files are supported", ife); throw new UnsupportedOperationException("Only .xlsx Excel 2007 OOXML files are supported", ife); } catch (OpenXML4JException | SAXException e) { getLogger().error("Error occurred while processing Excel document metadata", e); } } }); session.transfer(flowFile, ORIGINAL); } catch (RuntimeException ex) { getLogger().error("Failed to process incoming Excel document. " + ex.getMessage(), ex); FlowFile failedFlowFile = session.putAttribute(flowFile, ConvertExcelToCSVProcessor.class.getName() + ".error", ex.getMessage()); session.transfer(failedFlowFile, FAILURE); } }
From source file:org.apache.tika.parser.microsoft.ooxml.XSSFExcelExtractorDecorator.java
License:Apache License
/** * @see org.apache.poi.xssf.extractor.XSSFExcelExtractor#getText() */// w w w . j av a2 s .c o m @Override protected void buildXHTML(XHTMLContentHandler xhtml) throws SAXException, XmlException, IOException { OPCPackage container = extractor.getPackage(); ReadOnlySharedStringsTable strings; XSSFReader.SheetIterator iter; XSSFReader xssfReader; StylesTable styles; try { xssfReader = new XSSFReader(container); styles = xssfReader.getStylesTable(); iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData(); strings = new ReadOnlySharedStringsTable(container); } catch (InvalidFormatException e) { throw new XmlException(e); } catch (OpenXML4JException oe) { throw new XmlException(oe); } while (iter.hasNext()) { InputStream stream = iter.next(); sheetParts.add(iter.getSheetPart()); SheetTextAsHTML sheetExtractor = new SheetTextAsHTML(xhtml); CommentsTable comments = iter.getSheetComments(); // Start, and output the sheet name xhtml.startElement("div"); xhtml.element("h1", iter.getSheetName()); // Extract the main sheet contents xhtml.startElement("table"); xhtml.startElement("tbody"); processSheet(sheetExtractor, comments, styles, strings, stream); xhtml.endElement("tbody"); xhtml.endElement("table"); // Output any headers and footers // (Need to process the sheet to get them, so we can't // do the headers before the contents) for (String header : sheetExtractor.headers) { extractHeaderFooter(header, xhtml); } for (String footer : sheetExtractor.footers) { extractHeaderFooter(footer, xhtml); } processShapes(iter.getShapes(), xhtml); // All done with this sheet xhtml.endElement("div"); } }
From source file:org.dhatim.fastexcel.reader.BenchmarksTest.java
License:Apache License
@Benchmark public int streamingApachePoi() throws IOException, OpenXML4JException, SAXException { try (OPCPackage pkg = OPCPackage.open(openResource(FILE))) { ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(pkg); XSSFReader reader = new XSSFReader(pkg); StylesTable styles = reader.getStylesTable(); XSSFReader.SheetIterator iterator = (XSSFReader.SheetIterator) reader.getSheetsData(); int sheetIndex = 0; while (iterator.hasNext()) { try (InputStream sheetStream = iterator.next()) { if (sheetIndex == 0) { SheetContentHandler sheetHandler = new SheetContentHandler(); processSheet(styles, strings, sheetHandler, sheetStream); assertEquals(RESULT, sheetHandler.result); }//from w w w . j a v a 2 s . com } sheetIndex++; } return sheetIndex; } }
From source file:org.dhatim.fastexcel.reader.ReadableWorkbook.java
License:Apache License
private ReadableWorkbook(OPCPackage pkg) throws IOException { try {/*from w ww . j av a 2 s .com*/ this.pkg = pkg; reader = new XSSFReader(pkg); sst = reader.getSharedStringsTable(); } catch (NotOfficeXmlFileException | OpenXML4JException e) { throw new ExcelReaderException(e); } factory = XMLInputFactory.newInstance(); // To prevent XML External Entity (XXE) attacks factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE); try (SimpleXmlReader workbookReader = new SimpleXmlReader(factory, reader.getWorkbookData())) { readWorkbook(workbookReader); } catch (InvalidFormatException | XMLStreamException e) { throw new ExcelReaderException(e); } }
From source file:org.eclipse.birt.report.data.oda.excel.impl.util.XlsxFileReader.java
License:Open Source License
public XlsxFileReader(InputStream fis) throws IOException, OpenXML4JException { OPCPackage pkg = OPCPackage.open(fis); reader = new XSSFReader(pkg); }