org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable Java Examples
The following examples show how to use
org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XSSFUnmarshaller.java From poiji with MIT License | 6 votes |
private <T> void processSheet(StylesTable styles, XMLReader reader, ReadOnlySharedStringsTable readOnlySharedStringsTable, Class<T> type, InputStream sheetInputStream, Consumer<? super T> consumer) { DataFormatter formatter = new DataFormatter(); InputSource sheetSource = new InputSource(sheetInputStream); try { PoijiHandler<T> poijiHandler = new PoijiHandler<>(type, options, consumer); ContentHandler contentHandler = new XSSFSheetXMLPoijiHandler(styles, null, readOnlySharedStringsTable, poijiHandler, formatter, false, options); reader.setContentHandler(contentHandler); reader.parse(sheetSource); } catch (SAXException | IOException e) { IOUtils.closeQuietly(sheetInputStream); throw new PoijiException("Problem occurred while reading data", e); } }
Example #2
Source File: XLSX2CSV.java From DBus with Apache License 2.0 | 6 votes |
/** * Initiates the processing of the XLS workbook file to CSV. * * @throws IOException * @throws OpenXML4JException * @throws ParserConfigurationException * @throws SAXException */ public void 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(); //this.output.println(); //this.output.println(sheetName + " [index=" + index + "]:"); processSheet(styles, strings, stream); stream.close(); //++index; } }
Example #3
Source File: Xlsx2TmxHelper.java From tmxeditor8 with GNU General Public License v2.0 | 6 votes |
public void parseXlsxFileAndWriteTmxBody(String fileName, AbstractWriter tmxWriter, IProgressMonitor monitor) throws ParserConfigurationException, SAXException, IOException, OpenXML4JException { this.tmxWriter = tmxWriter; this.monitor = monitor; File file = new File(fileName); long length = file.length(); monitor.beginTask("", countTotal(length)); OPCPackage p = OPCPackage.open(fileName, PackageAccess.READ); ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(p); XSSFReader xssfReader = new XSSFReader(p); XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData(); try { while (iter.hasNext()) { InputStream stream = iter.next(); parse(stream, strings, tmxWriter); stream.close(); // 目前只处理第一个sheet break; } } finally { p.close(); } monitor.done(); }
Example #4
Source File: XLSX2CSV.java From azeroth with Apache License 2.0 | 6 votes |
/** * Initiates the processing of the XLS workbook file to CSV. * * @throws IOException * @throws OpenXML4JException * @throws ParserConfigurationException * @throws SAXException */ public List<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()) { if (blankRowNum == 10) { break; } InputStream stream = iter.next(); String sheetName = iter.getSheetName(); results.add(ExcelValidator.SHEET_NAME_PREFIX + sheetName); processSheet(styles, strings, new SheetToCSV(), stream); stream.close(); ++index; } return results; }
Example #5
Source File: XlsxRowReader.java From translationstudio8 with GNU General Public License v2.0 | 6 votes |
public void readRows(IProgressMonitor monitor) throws ParserConfigurationException, SAXException, IOException, OpenXML4JException { monitor.beginTask("", 10); monitor.worked(1); OPCPackage p = OPCPackage.open(xlsxFile, PackageAccess.READ); ReadOnlySharedStringsTable shareString = new ReadOnlySharedStringsTable(p); XSSFReader xssfReader = new XSSFReader(p); XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData(); try { while (iter.hasNext()) { InputStream stream = iter.next(); readCells(stream, shareString, new SubProgressMonitor(monitor, 9)); stream.close(); // 目前只处理第一个sheet break; } } finally { p.close(); monitor.done(); } }
Example #6
Source File: XLSX2CSV.java From bdf3 with Apache License 2.0 | 6 votes |
public void 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(); this.output.println(); this.output.println(sheetName + " [index=" + index + "]:"); processSheet(styles, strings, stream); stream.close(); ++index; } close(); }
Example #7
Source File: XLSX2CSV.java From jeesuite-libs with Apache License 2.0 | 6 votes |
/** * Initiates the processing of the XLS workbook file to CSV. * * @throws IOException * @throws OpenXML4JException * @throws ParserConfigurationException * @throws SAXException */ public List<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()) { if(blankRowNum == 10)break; InputStream stream = iter.next(); String sheetName = iter.getSheetName(); results.add(ExcelValidator.SHEET_NAME_PREFIX + sheetName); processSheet(styles, strings, new SheetToCSV(), stream); stream.close(); ++index; } return results; }
Example #8
Source File: Xlsx2TmxHelper.java From translationstudio8 with GNU General Public License v2.0 | 6 votes |
public void parseXlsxFileAndWriteTmxBody(String fileName, AbstractWriter tmxWriter, IProgressMonitor monitor) throws ParserConfigurationException, SAXException, IOException, OpenXML4JException { this.tmxWriter = tmxWriter; this.monitor = monitor; File file = new File(fileName); long length = file.length(); monitor.beginTask("", countTotal(length)); OPCPackage p = OPCPackage.open(fileName, PackageAccess.READ); ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(p); XSSFReader xssfReader = new XSSFReader(p); XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData(); try { while (iter.hasNext()) { InputStream stream = iter.next(); parse(stream, strings, tmxWriter); stream.close(); // 目前只处理第一个sheet break; } } finally { p.close(); } monitor.done(); }
Example #9
Source File: ExcelReader.java From excelReader with MIT License | 5 votes |
/** * Parses the content of one sheet using the specified styles and shared-strings tables. * * @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)); }
Example #10
Source File: ConvertExcelToCSVProcessor.java From nifi with Apache License 2.0 | 5 votes |
public ExcelSheetReadConfig(List<Integer> columnsToSkip, int overrideFirstRow, String sheetName, boolean formatValues, ReadOnlySharedStringsTable sst, StylesTable styles){ this.sheetName = sheetName; this.columnsToSkip = columnsToSkip; this.overrideFirstRow = overrideFirstRow; this.formatValues = formatValues; this.sst = sst; this.styles = styles; }
Example #11
Source File: Xlsx2TmxHelper.java From tmxeditor8 with GNU General Public License v2.0 | 5 votes |
public void parse(InputStream sheetInputStream, ReadOnlySharedStringsTable sharedStringsTable, AbstractWriter tmxWriter) throws ParserConfigurationException, SAXException, IOException { InputSource sheetSource = new InputSource(sheetInputStream); SAXParserFactory saxFactory = SAXParserFactory.newInstance(); SAXParser saxParser = saxFactory.newSAXParser(); XMLReader sheetParser = saxParser.getXMLReader(); ContentHandler handler = new XSSFHander(sharedStringsTable); sheetParser.setContentHandler(handler); sheetParser.parse(sheetSource); if (langCodes.isEmpty()) { throw new SAXException("EMPTY-LANG-CODE"); } writeEnd(); }
Example #12
Source File: StreamingSheetTest.java From data-prep with Apache License 2.0 | 5 votes |
@Before public void setUp() throws Exception { OPCPackage pkg = OPCPackage.open(StreamingSheetTest.class.getResourceAsStream("../dates.xlsx")); XSSFReader reader = new XSSFReader(pkg); ReadOnlySharedStringsTable sst = new ReadOnlySharedStringsTable(pkg); StylesTable styles = reader.getStylesTable(); Iterator<InputStream> iter = reader.getSheetsData(); XMLEventReader parser = XMLInputFactory.newInstance().createXMLEventReader(iter.next()); final StreamingSheetReader streamingSheetReader = new StreamingSheetReader(sst, styles, parser, 10); streamingSheet = new StreamingSheet("name", streamingSheetReader); }
Example #13
Source File: StreamingWorkbookReader.java From data-prep with Apache License 2.0 | 5 votes |
void loadSheets(XSSFReader reader, ReadOnlySharedStringsTable sst, StylesTable stylesTable, int rowCacheSize) throws IOException, InvalidFormatException, XMLStreamException { lookupSheetNames(reader.getWorkbookData()); Iterator<InputStream> iter = reader.getSheetsData(); int i = 0; while (iter.hasNext()) { XMLEventReader parser = XMLInputFactory.newInstance().createXMLEventReader(iter.next()); sheets.add(new StreamingSheet(sheetNames.get(i++), new StreamingSheetReader(sst, stylesTable, parser, rowCacheSize))); } }
Example #14
Source File: StreamingSheetReader.java From data-prep with Apache License 2.0 | 5 votes |
public StreamingSheetReader(ReadOnlySharedStringsTable sst, StylesTable stylesTable, XMLEventReader parser, int rowCacheSize) { this.sst = sst; this.stylesTable = stylesTable; this.parser = parser; this.rowCacheSize = rowCacheSize; }
Example #15
Source File: XlsxRowReader.java From translationstudio8 with GNU General Public License v2.0 | 5 votes |
private void readCells(InputStream sheetInputStream, ReadOnlySharedStringsTable sharedStringsTable, IProgressMonitor monitor) throws ParserConfigurationException, SAXException, IOException { InputSource sheetSource = new InputSource(sheetInputStream); SAXParserFactory saxFactory = SAXParserFactory.newInstance(); SAXParser saxParser = saxFactory.newSAXParser(); XMLReader sheetParser = saxParser.getXMLReader(); ContentHandler handler = new XSSFHander(sharedStringsTable, monitor); sheetParser.setContentHandler(handler); sheetParser.parse(sheetSource); rowsHandler.handleRows(rowCache); rowCache.clear(); }
Example #16
Source File: Xlsx2TmxHelper.java From translationstudio8 with GNU General Public License v2.0 | 5 votes |
public void parse(InputStream sheetInputStream, ReadOnlySharedStringsTable sharedStringsTable, AbstractWriter tmxWriter) throws ParserConfigurationException, SAXException, IOException { InputSource sheetSource = new InputSource(sheetInputStream); SAXParserFactory saxFactory = SAXParserFactory.newInstance(); SAXParser saxParser = saxFactory.newSAXParser(); XMLReader sheetParser = saxParser.getXMLReader(); ContentHandler handler = new XSSFHander(sharedStringsTable); sheetParser.setContentHandler(handler); sheetParser.parse(sheetSource); if(langCodes.isEmpty()){ throw new SAXException("EMPTY-LANG-CODE"); } writeEnd(); }
Example #17
Source File: XLSX2CSV.java From jeesuite-libs with Apache License 2.0 | 5 votes |
/** * Parses and shows the content of one sheet using the specified styles and * shared-strings tables. * * @param styles * @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()); } }
Example #18
Source File: XLSX2CSV.java From bdf3 with Apache License 2.0 | 5 votes |
public void processSheet(StylesTable styles, ReadOnlySharedStringsTable strings, InputStream sheetInputStream) throws IOException, ParserConfigurationException, SAXException { InputSource sheetSource = new InputSource(sheetInputStream); SAXParserFactory saxFactory = SAXParserFactory.newInstance(); SAXParser saxParser = saxFactory.newSAXParser(); XMLReader sheetParser = saxParser.getXMLReader(); ContentHandler handler = new MyXSSFSheetHandler(styles, strings, this.minColumns, this.output); sheetParser.setContentHandler(handler); sheetParser.parse(sheetSource); }
Example #19
Source File: XSSFExcelPolicy.java From bdf3 with Apache License 2.0 | 5 votes |
@Override public void apply(XSSFContext context) throws Exception { OPCPackage xlsxPackage = OPCPackage.open(context.getInpuStream()); XSSFReader xssfReader = new XSSFReader(xlsxPackage); context.setStyles(xssfReader.getStylesTable()); context.setStrings(new ReadOnlySharedStringsTable(xlsxPackage)); initContext(context); XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader .getSheetsData(); while (iter.hasNext()) { InputStream stream = iter.next(); context.setInpuStream(stream); String sheetName = context.getImporterSolution().getExcelSheetName(); if (StringUtils.isNotEmpty(sheetName)) { if (sheetName.equals(iter.getSheetName())) { sheetPolicy.apply(context); break; } } else { sheetPolicy.apply(context); } stream.close(); } parseRecordPolicy.apply(context); }
Example #20
Source File: XLSX2CSV.java From azeroth with Apache License 2.0 | 5 votes |
/** * Parses and shows the content of one sheet using the specified styles and * shared-strings tables. * * @param styles * @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()); } }
Example #21
Source File: XLSX2CSV.java From DBus with Apache License 2.0 | 5 votes |
/** * Parses and shows the content of one sheet using the specified styles and * shared-strings tables. * * @param styles * @param strings * @param sheetInputStream */ public void processSheet(StylesTable styles, ReadOnlySharedStringsTable strings, InputStream sheetInputStream) throws IOException, ParserConfigurationException, SAXException { InputSource sheetSource = new InputSource(sheetInputStream); SAXParserFactory saxFactory = SAXParserFactory.newInstance(); SAXParser saxParser = saxFactory.newSAXParser(); XMLReader sheetParser = saxParser.getXMLReader(); ContentHandler handler = new MyXSSFSheetHandler(styles, strings, this.minColumns, this.output); sheetParser.setContentHandler(handler); sheetParser.parse(sheetSource); }
Example #22
Source File: XLSX2CSV.java From DBus with Apache License 2.0 | 5 votes |
/** * Accepts objects needed while parsing. * * @param styles Table of styles * @param strings Table of shared strings * @param cols Minimum number of columns to show * @param target Sink for output */ public MyXSSFSheetHandler(StylesTable styles, ReadOnlySharedStringsTable strings, int cols, PrintStream target) { this.stylesTable = styles; this.sharedStringsTable = strings; this.minColumnCount = cols; this.output = target; this.value = new StringBuffer(); this.nextDataType = xssfDataType.NUMBER; this.formatter = new DataFormatter(); }
Example #23
Source File: Xlsx2TmxHelper.java From translationstudio8 with GNU General Public License v2.0 | 4 votes |
/** * */ public XSSFHander(ReadOnlySharedStringsTable sharedStringsTable) { this.sharedStringsTable = sharedStringsTable; }
Example #24
Source File: XlsxRowReader.java From translationstudio8 with GNU General Public License v2.0 | 4 votes |
public XSSFHander(ReadOnlySharedStringsTable sharedStringsTable, IProgressMonitor monitor) { this.sharedStringsTable = sharedStringsTable; this.monitor = monitor; }
Example #25
Source File: XSSFContext.java From bdf3 with Apache License 2.0 | 4 votes |
public void setStrings(ReadOnlySharedStringsTable strings) { this.strings = strings; }
Example #26
Source File: XSSFContext.java From bdf3 with Apache License 2.0 | 4 votes |
public ReadOnlySharedStringsTable getStrings() { return strings; }
Example #27
Source File: ReaderUtil.java From zerocell with Apache License 2.0 | 4 votes |
/** * Processes data from an Excel file contained in the OPCPackage using the * reader implementation * <p> * Please note that the process will read data from the first sheet in the * File when if sheet name is not specified * (i.e. the sheet name defaults to the {@link EntityHandler.DEFAULT_SHEET}) * </p> * @param opcPackage the OpenXML OPC Package * @param sheetName The sheet name * @param reader the reader implementation that handles the entity mapping */ private static void process(OPCPackage opcPackage, String sheetName, ZeroCellReader reader) { try { DataFormatter dataFormatter = new DataFormatter(); ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(opcPackage); XSSFReader xssfReader = new XSSFReader(opcPackage); StylesTable stylesTable = xssfReader.getStylesTable(); InputStream sheetInputStream = null; XSSFReader.SheetIterator sheets = (XSSFReader.SheetIterator) xssfReader.getSheetsData(); while (sheets.hasNext()) { sheetInputStream = sheets.next(); if (EntityHandler.DEFAULT_SHEET.equalsIgnoreCase(sheetName)) { break; } if (sheets.getSheetName().equalsIgnoreCase(sheetName)) { break; } else { sheetInputStream = null; } } if (Objects.isNull(sheetInputStream)) { throw new SheetNotFoundException(sheetName); } XMLReader xmlReader = SAXHelper.newXMLReader(); xmlReader.setContentHandler(new XSSFSheetXMLHandler(stylesTable, strings, reader, dataFormatter, false)); xmlReader.parse(new InputSource(sheetInputStream)); sheetInputStream.close(); xmlReader = null; sheetInputStream = null; stylesTable = null; strings = null; xssfReader = null; } catch(InvalidFormatException | EmptyFileException | NotOfficeXmlFileException ife) { throw new ZeroCellException(ERROR_NOT_OPENXML); } catch(SheetNotFoundException ex) { throw new ZeroCellException(ex.getMessage()); } catch (ZeroCellException ze) { throw ze; // Rethrow the Exception } catch (Exception e) { throw new ZeroCellException("Failed to process file", e); } }
Example #28
Source File: Xlsx2TmxHelper.java From tmxeditor8 with GNU General Public License v2.0 | 4 votes |
/** * */ public XSSFHander(ReadOnlySharedStringsTable sharedStringsTable) { this.sharedStringsTable = sharedStringsTable; }
Example #29
Source File: ConvertExcelToCSVProcessor.java From nifi with Apache License 2.0 | 4 votes |
public ReadOnlySharedStringsTable getSharedStringsTable(){ return sst; }
Example #30
Source File: XLSX2CSV.java From bdf3 with Apache License 2.0 | 3 votes |
/** * Accepts objects needed while parsing. * * @param styles * Table of styles * @param strings * Table of shared strings * @param cols * Minimum number of columns to show * @param target * Sink for output */ public MyXSSFSheetHandler(StylesTable styles, ReadOnlySharedStringsTable strings, int cols, PrintStream target) { this.stylesTable = styles; this.sharedStringsTable = strings; this.minColumnCount = cols; this.output = target; this.value = new StringBuffer(); this.nextDataType = xssfDataType.NUMBER; this.formatter = new DataFormatter(); }