Search in sources :

Example 16 with StylesTable

use of org.apache.poi.xssf.model.StylesTable in project poi by apache.

the class XSSFWorkbook method onDocumentRead.

@Override
protected void onDocumentRead() throws IOException {
    try {
        WorkbookDocument doc = WorkbookDocument.Factory.parse(getPackagePart().getInputStream(), DEFAULT_XML_OPTIONS);
        this.workbook = doc.getWorkbook();
        ThemesTable theme = null;
        Map<String, XSSFSheet> shIdMap = new HashMap<String, XSSFSheet>();
        Map<String, ExternalLinksTable> elIdMap = new HashMap<String, ExternalLinksTable>();
        for (RelationPart rp : getRelationParts()) {
            POIXMLDocumentPart p = rp.getDocumentPart();
            if (p instanceof SharedStringsTable) {
                sharedStringSource = (SharedStringsTable) p;
            } else if (p instanceof StylesTable) {
                stylesSource = (StylesTable) p;
            } else if (p instanceof ThemesTable) {
                theme = (ThemesTable) p;
            } else if (p instanceof CalculationChain) {
                calcChain = (CalculationChain) p;
            } else if (p instanceof MapInfo) {
                mapInfo = (MapInfo) p;
            } else if (p instanceof XSSFSheet) {
                shIdMap.put(rp.getRelationship().getId(), (XSSFSheet) p);
            } else if (p instanceof ExternalLinksTable) {
                elIdMap.put(rp.getRelationship().getId(), (ExternalLinksTable) p);
            }
        }
        boolean packageReadOnly = (getPackage().getPackageAccess() == PackageAccess.READ);
        if (stylesSource == null) {
            // Create Styles if it is missing
            if (packageReadOnly) {
                stylesSource = new StylesTable();
            } else {
                stylesSource = (StylesTable) createRelationship(XSSFRelation.STYLES, XSSFFactory.getInstance());
            }
        }
        stylesSource.setWorkbook(this);
        stylesSource.setTheme(theme);
        if (sharedStringSource == null) {
            // Create SST if it is missing
            if (packageReadOnly) {
                sharedStringSource = new SharedStringsTable();
            } else {
                sharedStringSource = (SharedStringsTable) createRelationship(XSSFRelation.SHARED_STRINGS, XSSFFactory.getInstance());
            }
        }
        // Load individual sheets. The order of sheets is defined by the order
        //  of CTSheet elements in the workbook
        sheets = new ArrayList<XSSFSheet>(shIdMap.size());
        for (CTSheet ctSheet : this.workbook.getSheets().getSheetArray()) {
            parseSheet(shIdMap, ctSheet);
        }
        // Load the external links tables. Their order is defined by the order 
        //  of CTExternalReference elements in the workbook
        externalLinks = new ArrayList<ExternalLinksTable>(elIdMap.size());
        if (this.workbook.isSetExternalReferences()) {
            for (CTExternalReference er : this.workbook.getExternalReferences().getExternalReferenceArray()) {
                ExternalLinksTable el = elIdMap.get(er.getId());
                if (el == null) {
                    logger.log(POILogger.WARN, "ExternalLinksTable with r:id " + er.getId() + " was defined, but didn't exist in package, skipping");
                    continue;
                }
                externalLinks.add(el);
            }
        }
        // Process the named ranges
        reprocessNamedRanges();
    } catch (XmlException e) {
        throw new POIXMLException(e);
    }
}
Also used : WorkbookDocument(org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument) ThemesTable(org.apache.poi.xssf.model.ThemesTable) HashMap(java.util.HashMap) ArrayListValuedHashMap(org.apache.commons.collections4.multimap.ArrayListValuedHashMap) CTExternalReference(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTExternalReference) POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) StylesTable(org.apache.poi.xssf.model.StylesTable) POIXMLException(org.apache.poi.POIXMLException) CalculationChain(org.apache.poi.xssf.model.CalculationChain) SharedStringsTable(org.apache.poi.xssf.model.SharedStringsTable) CTSheet(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet) XmlException(org.apache.xmlbeans.XmlException) MapInfo(org.apache.poi.xssf.model.MapInfo) ExternalLinksTable(org.apache.poi.xssf.model.ExternalLinksTable)

Example 17 with StylesTable

use of org.apache.poi.xssf.model.StylesTable in project poi by apache.

the class TestXSSFWorkbook method getCellStyleAt.

@Test
public void getCellStyleAt() throws IOException {
    XSSFWorkbook workbook = new XSSFWorkbook();
    try {
        short i = 0;
        //get default style
        CellStyle cellStyleAt = workbook.getCellStyleAt(i);
        assertNotNull(cellStyleAt);
        //get custom style
        StylesTable styleSource = workbook.getStylesSource();
        XSSFCellStyle customStyle = new XSSFCellStyle(styleSource);
        XSSFFont font = new XSSFFont();
        font.setFontName("Verdana");
        customStyle.setFont(font);
        int x = styleSource.putStyle(customStyle);
        cellStyleAt = workbook.getCellStyleAt((short) x);
        assertNotNull(cellStyleAt);
    } finally {
        workbook.close();
    }
}
Also used : StylesTable(org.apache.poi.xssf.model.StylesTable) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Test(org.junit.Test)

Example 18 with StylesTable

use of org.apache.poi.xssf.model.StylesTable in project poi by apache.

the class TestXSSFWorkbook method styles.

@Test
public void styles() throws IOException {
    XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("Formatting.xlsx");
    StylesTable ss = wb1.getStylesSource();
    assertNotNull(ss);
    StylesTable st = ss;
    // Has 8 number formats
    assertEquals(8, st.getNumDataFormats());
    // Has 2 fonts
    assertEquals(2, st.getFonts().size());
    // Has 2 fills
    assertEquals(2, st.getFills().size());
    // Has 1 border
    assertEquals(1, st.getBorders().size());
    // Add two more styles
    assertEquals(StylesTable.FIRST_CUSTOM_STYLE_ID + 8, st.putNumberFormat("testFORMAT"));
    assertEquals(StylesTable.FIRST_CUSTOM_STYLE_ID + 8, st.putNumberFormat("testFORMAT"));
    assertEquals(StylesTable.FIRST_CUSTOM_STYLE_ID + 9, st.putNumberFormat("testFORMAT2"));
    assertEquals(10, st.getNumDataFormats());
    // Save, load back in again, and check
    XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
    wb1.close();
    ss = wb2.getStylesSource();
    assertNotNull(ss);
    assertEquals(10, st.getNumDataFormats());
    assertEquals(2, st.getFonts().size());
    assertEquals(2, st.getFills().size());
    assertEquals(1, st.getBorders().size());
    wb2.close();
}
Also used : StylesTable(org.apache.poi.xssf.model.StylesTable) Test(org.junit.Test)

Example 19 with StylesTable

use of org.apache.poi.xssf.model.StylesTable 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)

Example 20 with StylesTable

use of org.apache.poi.xssf.model.StylesTable in project poi by apache.

the class TestXSSFReader method testSheetWithNoRelationshipId.

/**
     * NPE when sheet has no relationship id in the workbook
     * 60825
     */
public void testSheetWithNoRelationshipId() throws Exception {
    OPCPackage pkg = XSSFTestDataSamples.openSamplePackage("60825.xlsx");
    ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(pkg);
    assertNotNull(strings);
    XSSFReader reader = new XSSFReader(pkg);
    StylesTable styles = reader.getStylesTable();
    assertNotNull(styles);
    XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) reader.getSheetsData();
    assertNotNull(iter.next());
    assertFalse(iter.hasNext());
    pkg.close();
}
Also used : StylesTable(org.apache.poi.xssf.model.StylesTable) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Aggregations

StylesTable (org.apache.poi.xssf.model.StylesTable)20 Test (org.junit.Test)8 InputStream (java.io.InputStream)4 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)4 ReadOnlySharedStringsTable (org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable)3 XSSFReader (org.apache.poi.xssf.eventusermodel.XSSFReader)3 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)2 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)2 CommentsTable (org.apache.poi.xssf.model.CommentsTable)2 ThemesTable (org.apache.poi.xssf.model.ThemesTable)2 XmlException (org.apache.xmlbeans.XmlException)2 CTWorksheet (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet)2 CTXf (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 ArrayListValuedHashMap (org.apache.commons.collections4.multimap.ArrayListValuedHashMap)1 POIXMLDocumentPart (org.apache.poi.POIXMLDocumentPart)1 POIXMLException (org.apache.poi.POIXMLException)1