List of usage examples for org.apache.poi.xssf.eventusermodel XSSFSheetXMLHandler XSSFSheetXMLHandler
public XSSFSheetXMLHandler(Styles styles, Comments comments, SharedStrings strings, SheetContentsHandler sheetContentsHandler, DataFormatter dataFormatter, boolean formulasNotResults)
From source file:com.jkoolcloud.tnt4j.streams.inputs.ExcelSXSSFRowStream.java
License:Apache License
/** * Parses the content of one sheet using the specified styles and shared-strings tables. * * @param styles/*from w ww. j a v a 2s . c o m*/ * the table of styles that may be referenced by cells in the sheet * @param strings * the table of strings that may be referenced by cells in the sheet * @param sheetHandler * the sheet handler * @param sheetInputStream * the input stream to read the sheet-data from * * @throws IOException * if sheet input stream read fails * @throws SAXException * if sheet input stream provided XML can't be parsed */ public static void processSXSSFSheet(StylesTable styles, ReadOnlySharedStringsTable strings, SheetContentsHandler sheetHandler, InputStream sheetInputStream) throws IOException, SAXException { DataFormatter formatter = new DataFormatter(); InputSource sheetSource = new InputSource(sheetInputStream); try { XMLReader sheetParser = SAXHelper.newXMLReader(); ContentHandler handler = new XSSFSheetXMLHandler(styles, null, strings, sheetHandler, formatter, false); sheetParser.setContentHandler(handler); sheetParser.parse(sheetSource); } catch (ParserConfigurationException exc) { throw new RuntimeException( StreamsResources.getStringFormatted(MsOfficeStreamConstants.RESOURCE_BUNDLE_NAME, "ExcelSXSSFRowStream.sax.cfg.error", Utils.getExceptionMessages(exc))); } }
From source file:com.lfwer.common.XLSX2CSV.java
License:Apache License
/** * Parses and shows the content of one sheet using the specified styles and * shared-strings tables./*from w ww . j ava2s. c o m*/ * * @param styles * The table of styles that may be referenced by cells in the * sheet * @param strings * The table of strings that may be referenced by cells in the * sheet * @param sheetInputStream * The stream to read the sheet-data from. * * @exception java.io.IOException * An IO exception from the parser, possibly from a byte * stream or character stream supplied by the application. * @throws SAXException * if parsing the XML data fails. */ public void processSheet(StylesTable styles, ReadOnlySharedStringsTable strings, SheetContentsHandler sheetHandler, InputStream sheetInputStream) throws IOException, SAXException { DataFormatter formatter = new DataFormatter(); InputSource sheetSource = new InputSource(sheetInputStream); try { XMLReader sheetParser = SAXHelper.newXMLReader(); ContentHandler handler = new XSSFSheetXMLHandler(styles, null, strings, sheetHandler, formatter, false); sheetParser.setContentHandler(handler); sheetParser.parse(sheetSource); } catch (ParserConfigurationException e) { throw new RuntimeException("SAX parser appears to be broken - " + e.getMessage()); } }
From source file:com.sonicle.webtop.contacts.io.input.ContactExcelFileReader.java
License:Open Source License
private void readXlsxContacts(File file, BeanHandler beanHandler) throws IOException, FileReaderException { OPCPackage opc = null;/* w w w. ja va2 s.c om*/ HashMap<String, Integer> columnIndexes = listXlsxColumnIndexes(file); XlsRowHandler rowHandler = new XlsRowHandler(this, columnIndexes, beanHandler); try { opc = OPCPackage.open(file, PackageAccess.READ); XSSFReader reader = new XSSFReader(opc); ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(opc); StylesTable styles = reader.getStylesTable(); XlsxRowsHandler rowsHandler = null; XSSFReader.SheetIterator sit = (XSSFReader.SheetIterator) reader.getSheetsData(); while (sit.hasNext()) { InputStream is = null; try { is = sit.next(); if (StringUtils.equals(sit.getSheetName(), sheet)) { XMLReader xmlReader = SAXHelper.newXMLReader(); rowsHandler = new XlsxRowsHandler(is, headersRow, firstDataRow, lastDataRow, rowHandler); ContentHandler xhandler = new XSSFSheetXMLHandler(styles, null, strings, rowsHandler, fmt, false); xmlReader.setContentHandler(xhandler); xmlReader.parse(new InputSource(is)); } } catch (SAXException | ParserConfigurationException ex) { throw new FileReaderException(ex, "Error processing file content"); } catch (NullPointerException ex) { // Thrown when stream is forcibly closed. Simply ignore this! } finally { IOUtils.closeQuietly(is); } if (rowsHandler != null) break; } } catch (OpenXML4JException | SAXException ex) { throw new FileReaderException(ex, "Error opening file"); } finally { IOUtils.closeQuietly(opc); } }
From source file:com.sonicle.webtop.core.io.input.ExcelFileReader.java
License:Open Source License
public HashMap<String, String> listXlsxColumnNames(File file) throws IOException, FileReaderException { OPCPackage opc = null;//w ww. ja v a2s .c o m try { opc = OPCPackage.open(file, PackageAccess.READ); XSSFReader reader = new XSSFReader(opc); ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(opc); StylesTable styles = reader.getStylesTable(); XlsxColumnsHandler columnsHandler = null; XSSFReader.SheetIterator sit = (XSSFReader.SheetIterator) reader.getSheetsData(); while (sit.hasNext()) { InputStream is = null; try { is = sit.next(); if (StringUtils.equals(sit.getSheetName(), sheet)) { XMLReader xmlReader = SAXHelper.newXMLReader(); columnsHandler = new XlsxColumnsHandler(is, headersRow, firstDataRow, lastDataRow); ContentHandler handler = new XSSFSheetXMLHandler(styles, null, strings, columnsHandler, fmt, false); xmlReader.setContentHandler(handler); xmlReader.parse(new InputSource(is)); } } catch (SAXException | ParserConfigurationException ex) { throw new FileReaderException(ex, "Error processing file content"); } catch (NullPointerException ex) { // Thrown when stream is forcibly closed. Simply ignore this! } finally { IOUtils.closeQuietly(is); } if (columnsHandler != null) break; } return columnsHandler.columnNames; } catch (OpenXML4JException | SAXException ex) { throw new FileReaderException(ex, "Error opening file"); } finally { IOUtils.closeQuietly(opc); } }
From source file:com.sonicle.webtop.core.io.input.ExcelFileReader.java
License:Open Source License
public HashMap<String, Integer> listXlsxColumnIndexes(File file) throws IOException, FileReaderException { OPCPackage opc = null;//www . jav a 2 s . c o m try { opc = OPCPackage.open(file, PackageAccess.READ); XSSFReader reader = new XSSFReader(opc); ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(opc); StylesTable styles = reader.getStylesTable(); XlsxColumnsHandler columnsHandler = null; XSSFReader.SheetIterator sit = (XSSFReader.SheetIterator) reader.getSheetsData(); while (sit.hasNext()) { InputStream is = null; try { is = sit.next(); if (StringUtils.equals(sit.getSheetName(), sheet)) { XMLReader xmlReader = SAXHelper.newXMLReader(); columnsHandler = new XlsxColumnsHandler(is, headersRow, firstDataRow, lastDataRow); ContentHandler handler = new XSSFSheetXMLHandler(styles, null, strings, columnsHandler, fmt, false); xmlReader.setContentHandler(handler); xmlReader.parse(new InputSource(is)); } } catch (SAXException | ParserConfigurationException ex) { throw new FileReaderException(ex, "Error processing file content"); } catch (NullPointerException ex) { // Thrown when stream is forcibly closed. Simply ignore this! } finally { IOUtils.closeQuietly(is); } if (columnsHandler != null) break; } return columnsHandler.columnIndexes; } catch (OpenXML4JException | SAXException ex) { throw new FileReaderException(ex, "Error opening file"); } finally { IOUtils.closeQuietly(opc); } }
From source file:com.test.demo.ccbpay.XLSX2CSV.java
License:Apache License
/** * Parses and shows the content of one sheet * using the specified styles and shared-strings tables. * * @param styles/*from w w w . j av a 2 s . c o m*/ * @param strings * @param sheetInputStream */ public void processSheet(StylesTable styles, ReadOnlySharedStringsTable strings, SheetContentsHandler sheetHandler, InputStream sheetInputStream) throws IOException, ParserConfigurationException, SAXException { DataFormatter formatter = new DataFormatter(); InputSource sheetSource = new InputSource(sheetInputStream); try { XMLReader sheetParser = SAXHelper.newXMLReader(); ContentHandler handler = new XSSFSheetXMLHandler(styles, null, strings, sheetHandler, formatter, false); sheetParser.setContentHandler(handler); sheetParser.parse(sheetSource); } catch (ParserConfigurationException e) { throw new RuntimeException("SAX parser appears to be broken - " + e.getMessage()); } }
From source file:org.apache.nifi.processors.poi.ConvertExcelToCSVProcessor.java
License:Apache License
/** * Handles an individual Excel sheet from the entire Excel document. Each sheet will result in an individual flowfile. * * @param session//from w w w.ja v a 2 s . c o m * The NiFi ProcessSession instance for the current invocation. */ private void handleExcelSheet(ProcessSession session, FlowFile originalParentFF, final InputStream sheetInputStream, ExcelSheetReadConfig readConfig, CSVFormat csvFormat) throws IOException { FlowFile ff = session.create(); try { final DataFormatter formatter = new DataFormatter(); final InputSource sheetSource = new InputSource(sheetInputStream); final SheetToCSV sheetHandler = new SheetToCSV(readConfig, csvFormat); final XMLReader parser = SAXHelper.newXMLReader(); //If Value Formatting is set to false then don't pass in the styles table. // This will cause the XSSF Handler to return the raw value instead of the formatted one. final StylesTable sst = readConfig.getFormatValues() ? readConfig.getStyles() : null; final XSSFSheetXMLHandler handler = new XSSFSheetXMLHandler(sst, null, readConfig.getSharedStringsTable(), sheetHandler, formatter, false); parser.setContentHandler(handler); ff = session.write(ff, new OutputStreamCallback() { @Override public void process(OutputStream out) throws IOException { PrintStream outPrint = new PrintStream(out); sheetHandler.setOutput(outPrint); try { parser.parse(sheetSource); sheetInputStream.close(); sheetHandler.close(); outPrint.close(); } catch (SAXException se) { getLogger().error("Error occurred while processing Excel sheet {}", new Object[] { readConfig.getSheetName() }, se); } } }); ff = session.putAttribute(ff, SHEET_NAME, readConfig.getSheetName()); ff = session.putAttribute(ff, ROW_NUM, new Long(sheetHandler.getRowCount()).toString()); if (StringUtils.isNotEmpty(originalParentFF.getAttribute(CoreAttributes.FILENAME.key()))) { ff = session.putAttribute(ff, SOURCE_FILE_NAME, originalParentFF.getAttribute(CoreAttributes.FILENAME.key())); } else { ff = session.putAttribute(ff, SOURCE_FILE_NAME, UNKNOWN_SHEET_NAME); } //Update the CoreAttributes.FILENAME to have the .csv extension now. Also update MIME.TYPE ff = session.putAttribute(ff, CoreAttributes.FILENAME.key(), updateFilenameToCSVExtension(ff.getAttribute(CoreAttributes.UUID.key()), ff.getAttribute(CoreAttributes.FILENAME.key()), readConfig.getSheetName())); ff = session.putAttribute(ff, CoreAttributes.MIME_TYPE.key(), CSV_MIME_TYPE); session.transfer(ff, SUCCESS); } catch (SAXException | ParserConfigurationException saxE) { getLogger().error("Failed to create instance of Parser.", saxE); ff = session.putAttribute(ff, ConvertExcelToCSVProcessor.class.getName() + ".error", saxE.getMessage()); session.transfer(ff, FAILURE); } finally { sheetInputStream.close(); } }
From source file:org.apache.tika.parser.microsoft.ooxml.XSSFExcelExtractorDecorator.java
License:Apache License
public void processSheet(SheetContentsHandler sheetContentsExtractor, CommentsTable comments, StylesTable styles, ReadOnlySharedStringsTable strings, InputStream sheetInputStream) throws IOException, SAXException { InputSource sheetSource = new InputSource(sheetInputStream); SAXParserFactory saxFactory = SAXParserFactory.newInstance(); saxFactory.setNamespaceAware(true);/* w ww.j a va2s. c o m*/ try { SAXParser saxParser = saxFactory.newSAXParser(); XMLReader sheetParser = saxParser.getXMLReader(); XSSFSheetInterestingPartsCapturer handler = new XSSFSheetInterestingPartsCapturer( new XSSFSheetXMLHandler(styles, comments, strings, sheetContentsExtractor, formatter, false)); sheetParser.setContentHandler(handler); sheetParser.parse(sheetSource); sheetInputStream.close(); if (handler.hasProtection) { metadata.set(TikaMetadataKeys.PROTECTED, "true"); } } catch (ParserConfigurationException e) { throw new RuntimeException("SAX parser appears to be broken - " + e.getMessage()); } }
From source file:org.dhatim.fastexcel.reader.BenchmarksTest.java
License:Apache License
private void processSheet(StylesTable styles, ReadOnlySharedStringsTable strings, XSSFSheetXMLHandler.SheetContentsHandler sheetHandler, InputStream sheetInputStream) throws IOException, SAXException { DataFormatter formatter = new DataFormatter(); InputSource sheetSource = new InputSource(sheetInputStream); SAXParserFactory saxFactory = SAXParserFactory.newInstance(); saxFactory.setNamespaceAware(true);/*from ww w . j a va 2 s . co m*/ try { SAXParser saxParser = saxFactory.newSAXParser(); XMLReader sheetParser = saxParser.getXMLReader(); ContentHandler handler = new XSSFSheetXMLHandler(styles, null, strings, sheetHandler, formatter, false); sheetParser.setContentHandler(handler); sheetParser.parse(sheetSource); } catch (ParserConfigurationException e) { throw new IllegalStateException("SAX parser appears to be broken - " + e.getMessage()); } }