Search in sources :

Example 11 with XSSFReader

use of org.apache.poi.xssf.eventusermodel.XSSFReader in project poi by apache.

the class XSSFEventBasedExcelExtractor method getText.

/**
    * Processes the file and returns the text
    */
public String getText() {
    try {
        ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(container, concatenatePhoneticRuns);
        XSSFReader xssfReader = new XSSFReader(container);
        StylesTable styles = xssfReader.getStylesTable();
        XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
        StringBuffer text = new StringBuffer();
        SheetTextExtractor sheetExtractor = new SheetTextExtractor();
        while (iter.hasNext()) {
            InputStream stream = iter.next();
            if (includeSheetNames) {
                text.append(iter.getSheetName());
                text.append('\n');
            }
            CommentsTable comments = includeCellComments ? iter.getSheetComments() : null;
            processSheet(sheetExtractor, styles, comments, strings, stream);
            if (includeHeadersFooters) {
                sheetExtractor.appendHeaderText(text);
            }
            sheetExtractor.appendCellText(text);
            if (includeTextBoxes) {
                processShapes(iter.getShapes(), text);
            }
            if (includeHeadersFooters) {
                sheetExtractor.appendFooterText(text);
            }
            sheetExtractor.reset();
            stream.close();
        }
        return text.toString();
    } catch (IOException e) {
        LOGGER.log(POILogger.WARN, e);
        return null;
    } catch (SAXException se) {
        LOGGER.log(POILogger.WARN, se);
        return null;
    } catch (OpenXML4JException o4je) {
        LOGGER.log(POILogger.WARN, o4je);
        return null;
    }
}
Also used : ReadOnlySharedStringsTable(org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) InputStream(java.io.InputStream) StylesTable(org.apache.poi.xssf.model.StylesTable) IOException(java.io.IOException) XSSFReader(org.apache.poi.xssf.eventusermodel.XSSFReader) CommentsTable(org.apache.poi.xssf.model.CommentsTable) SAXException(org.xml.sax.SAXException)

Aggregations

XSSFReader (org.apache.poi.xssf.eventusermodel.XSSFReader)11 InputStream (java.io.InputStream)10 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)9 ReadOnlySharedStringsTable (org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable)5 InputSource (org.xml.sax.InputSource)4 XMLReader (org.xml.sax.XMLReader)4 BufferedInputStream (java.io.BufferedInputStream)3 FileInputStream (java.io.FileInputStream)3 StylesTable (org.apache.poi.xssf.model.StylesTable)3 IOException (java.io.IOException)2 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)2 CommentsTable (org.apache.poi.xssf.model.CommentsTable)2 SharedStringsTable (org.apache.poi.xssf.model.SharedStringsTable)2 SAXException (org.xml.sax.SAXException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)1