Java Code Examples for org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable

The following examples show how to use org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable. These examples are extracted from open source projects. 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 Project: poiji   Source File: XSSFUnmarshaller.java    License: MIT License 6 votes vote down vote up
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 Project: DBus   Source File: XLSX2CSV.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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 Project: azeroth   Source File: XLSX2CSV.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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 4
Source Project: bdf3   Source File: XLSX2CSV.java    License: Apache License 2.0 6 votes vote down vote up
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 5
Source Project: jeesuite-libs   Source File: XLSX2CSV.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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 6
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 7
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 8
Source Project: tmxeditor8   Source File: Xlsx2TmxHelper.java    License: GNU General Public License v2.0 6 votes vote down vote up
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 Project: DBus   Source File: XLSX2CSV.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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 10
Source Project: DBus   Source File: XLSX2CSV.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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 11
Source Project: azeroth   Source File: XLSX2CSV.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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 12
Source Project: bdf3   Source File: XSSFExcelPolicy.java    License: Apache License 2.0 5 votes vote down vote up
@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 13
Source Project: bdf3   Source File: XLSX2CSV.java    License: Apache License 2.0 5 votes vote down vote up
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 14
Source Project: jeesuite-libs   Source File: XLSX2CSV.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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 15
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 16
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 17
Source Project: data-prep   Source File: StreamingSheetReader.java    License: Apache License 2.0 5 votes vote down vote up
public StreamingSheetReader(ReadOnlySharedStringsTable sst, StylesTable stylesTable, XMLEventReader parser,
        int rowCacheSize) {
    this.sst = sst;
    this.stylesTable = stylesTable;
    this.parser = parser;
    this.rowCacheSize = rowCacheSize;
}
 
Example 18
Source Project: data-prep   Source File: StreamingWorkbookReader.java    License: Apache License 2.0 5 votes vote down vote up
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 19
Source Project: data-prep   Source File: StreamingSheetTest.java    License: Apache License 2.0 5 votes vote down vote up
@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 20
Source Project: tmxeditor8   Source File: Xlsx2TmxHelper.java    License: GNU General Public License v2.0 5 votes vote down vote up
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 21
Source Project: nifi   Source File: ConvertExcelToCSVProcessor.java    License: Apache License 2.0 5 votes vote down vote up
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 22
Source Project: excelReader   Source File: ExcelReader.java    License: MIT License 5 votes vote down vote up
/**
 * 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 23
Source Project: zerocell   Source File: ReaderUtil.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 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 24
Source Project: bdf3   Source File: XSSFContext.java    License: Apache License 2.0 4 votes vote down vote up
public ReadOnlySharedStringsTable getStrings() {
	return strings;
}
 
Example 25
Source Project: bdf3   Source File: XSSFContext.java    License: Apache License 2.0 4 votes vote down vote up
public void setStrings(ReadOnlySharedStringsTable strings) {
	this.strings = strings;
}
 
Example 26
/**
 * 
 */
public XSSFHander(ReadOnlySharedStringsTable sharedStringsTable) {
	this.sharedStringsTable = sharedStringsTable;

}
 
Example 27
public XSSFHander(ReadOnlySharedStringsTable sharedStringsTable, IProgressMonitor  monitor) {
	this.sharedStringsTable = sharedStringsTable;
	this.monitor = monitor;
}
 
Example 28
Source Project: tmxeditor8   Source File: Xlsx2TmxHelper.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * 
 */
public XSSFHander(ReadOnlySharedStringsTable sharedStringsTable) {
	this.sharedStringsTable = sharedStringsTable;

}
 
Example 29
Source Project: nifi   Source File: ConvertExcelToCSVProcessor.java    License: Apache License 2.0 4 votes vote down vote up
public ReadOnlySharedStringsTable getSharedStringsTable(){
    return sst;
}
 
Example 30
Source Project: bdf3   Source File: XLSX2CSV.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * 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();
}