Search in sources :

Example 11 with StylesTable

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

the class TestXSSFCellStyle method testBug55650.

/**
     * Avoid ArrayIndexOutOfBoundsException  when getting cell style
     * in a workbook that has an empty xf table.
     */
@Test
public void testBug55650() throws IOException {
    XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("52348.xlsx");
    StylesTable st = workbook.getStylesSource();
    assertEquals(0, st._getStyleXfsSize());
    // no exception at this point
    XSSFCellStyle style = workbook.getSheetAt(0).getRow(0).getCell(0).getCellStyle();
    assertNull(style.getStyleXf());
    XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(workbook);
    assertNotNull(wb2);
    wb2.close();
    workbook.close();
}
Also used : StylesTable(org.apache.poi.xssf.model.StylesTable) Test(org.junit.Test)

Example 12 with StylesTable

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

the class TestXSSFRichTextString method testApplyFontWithStyles.

public void testApplyFontWithStyles() {
    XSSFRichTextString rt = new XSSFRichTextString("Apache POI");
    StylesTable tbl = new StylesTable();
    rt.setStylesTableReference(tbl);
    try {
        rt.applyFont(0, 10, (short) 1);
        fail("Fails without styles in the table");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    tbl.putFont(new XSSFFont());
    rt.applyFont(0, 10, (short) 1);
    rt.applyFont((short) 1);
}
Also used : StylesTable(org.apache.poi.xssf.model.StylesTable)

Example 13 with StylesTable

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

the class XSSFExcelExtractorDecorator method buildXHTML.

/**
     * @see org.apache.poi.xssf.extractor.XSSFExcelExtractor#getText()
     */
@Override
protected void buildXHTML(XHTMLContentHandler xhtml) throws SAXException, XmlException, IOException {
    OPCPackage container = extractor.getPackage();
    ReadOnlySharedStringsTable strings;
    XSSFReader.SheetIterator iter;
    XSSFReader xssfReader;
    StylesTable styles;
    try {
        xssfReader = new XSSFReader(container);
        styles = xssfReader.getStylesTable();
        iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
        strings = new ReadOnlySharedStringsTable(container);
    } catch (InvalidFormatException e) {
        throw new XmlException(e);
    } catch (OpenXML4JException oe) {
        throw new XmlException(oe);
    }
    //temporary workaround for POI-61034
    //remove once POI 3.17-beta1 is released
    Set<String> seen = new HashSet<>();
    while (iter.hasNext()) {
        SheetTextAsHTML sheetExtractor = new SheetTextAsHTML(xhtml);
        PackagePart sheetPart = null;
        try (InputStream stream = iter.next()) {
            sheetPart = iter.getSheetPart();
            final String partName = sheetPart.getPartName().toString();
            if (seen.contains(partName)) {
                continue;
            }
            seen.add(partName);
            addDrawingHyperLinks(sheetPart);
            sheetParts.add(sheetPart);
            CommentsTable comments = iter.getSheetComments();
            // Start, and output the sheet name
            xhtml.startElement("div");
            xhtml.element("h1", iter.getSheetName());
            // Extract the main sheet contents
            xhtml.startElement("table");
            xhtml.startElement("tbody");
            processSheet(sheetExtractor, comments, styles, strings, stream);
        }
        xhtml.endElement("tbody");
        xhtml.endElement("table");
        //  do the headers before the contents)
        for (String header : sheetExtractor.headers) {
            extractHeaderFooter(header, xhtml);
        }
        for (String footer : sheetExtractor.footers) {
            extractHeaderFooter(footer, xhtml);
        }
        // Do text held in shapes, if required
        if (config.getIncludeShapeBasedContent()) {
            List<XSSFShape> shapes = iter.getShapes();
            processShapes(shapes, xhtml);
        }
        //for now dump sheet hyperlinks at bottom of page
        //consider a double-pass of the inputstream to reunite hyperlinks with cells/textboxes
        //step 1: extract hyperlink info from bottom of page
        //step 2: process as we do now, but with cached hyperlink relationship info
        extractHyperLinks(sheetPart, xhtml);
        // All done with this sheet
        xhtml.endElement("div");
    }
}
Also used : ReadOnlySharedStringsTable(org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable) InputStream(java.io.InputStream) StylesTable(org.apache.poi.xssf.model.StylesTable) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) CommentsTable(org.apache.poi.xssf.model.CommentsTable) XSSFShape(org.apache.poi.xssf.usermodel.XSSFShape) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) XmlException(org.apache.xmlbeans.XmlException) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) XSSFReader(org.apache.poi.xssf.eventusermodel.XSSFReader) HashSet(java.util.HashSet)

Example 14 with StylesTable

use of org.apache.poi.xssf.model.StylesTable in project tdi-studio-se by Talend.

the class ExcelReader method call.

public Object call() throws Exception {
    OPCPackage pkg = null;
    try {
        if (fileURL != null) {
            pkg = OPCPackage.open(fileURL);
        } else {
            pkg = PackageHelper.open(is);
        }
        XSSFReader r = new XSSFReader(pkg);
        StylesTable styles = r.getStylesTable();
        ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(pkg);
        sheetContentsHandler = new DefaultTalendSheetContentsHandler(cache);
        DataFormatter formatter = new DataFormatter();
        boolean formulasNotResults = false;
        XMLReader parser = XMLReaderFactory.createXMLReader();
        ContentHandler handler = new TalendXSSFSheetXMLHandler(styles, strings, sheetContentsHandler, formatter, formulasNotResults);
        parser.setContentHandler(handler);
        XSSFReader.SheetIterator sheets = (XSSFReader.SheetIterator) r.getSheetsData();
        //            List<InputStream> iss = new ArrayList<InputStream>();
        LinkedHashMap<String, InputStream> issmap = new LinkedHashMap<String, InputStream>();
        while (sheets.hasNext()) {
            InputStream sheet = sheets.next();
            String sheetName = sheets.getSheetName();
            boolean match = false;
            for (int i = 0; i < sheetNames.size(); i++) {
                if ((asRegexs.get(i) && sheetName.matches(sheetNames.get(i))) || (!asRegexs.get(i) && sheetName.equals(sheetNames.get(i)))) {
                    match = true;
                    //                        iss.add(sheet);
                    issmap.put(sheetName, sheet);
                    break;
                }
            }
            if (!match) {
                sheet.close();
            }
        }
        if (issmap.size() < 1) {
            throw new RuntimeException("No match sheets");
        }
        for (InputStream is : issmap.values()) {
            try {
                InputSource sheetSource = new InputSource(is);
                sheetSource.setEncoding(charset);
                parser.parse(sheetSource);
            } finally {
                is.close();
            }
        }
    } finally {
        if (pkg != null) {
            pkg.revert();
        }
        cache.notifyErrorOccurred();
    }
    return null;
}
Also used : ReadOnlySharedStringsTable(org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable) InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream) StylesTable(org.apache.poi.xssf.model.StylesTable) ContentHandler(org.xml.sax.ContentHandler) LinkedHashMap(java.util.LinkedHashMap) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) XMLReader(org.xml.sax.XMLReader) XSSFReader(org.apache.poi.xssf.eventusermodel.XSSFReader) DataFormatter(org.apache.poi.ss.usermodel.DataFormatter)

Example 15 with StylesTable

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

the class TestXSSFSheet method setDefaultColumnStyle.

@Test
public void setDefaultColumnStyle() throws IOException {
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet();
    CTWorksheet ctWorksheet = sheet.getCTWorksheet();
    StylesTable stylesTable = workbook.getStylesSource();
    XSSFFont font = new XSSFFont();
    font.setFontName("Cambria");
    stylesTable.putFont(font);
    CTXf cellStyleXf = CTXf.Factory.newInstance();
    cellStyleXf.setFontId(1);
    cellStyleXf.setFillId(0);
    cellStyleXf.setBorderId(0);
    cellStyleXf.setNumFmtId(0);
    stylesTable.putCellStyleXf(cellStyleXf);
    CTXf cellXf = CTXf.Factory.newInstance();
    cellXf.setXfId(1);
    stylesTable.putCellXf(cellXf);
    XSSFCellStyle cellStyle = new XSSFCellStyle(1, 1, stylesTable, null);
    assertEquals(1, cellStyle.getFontIndex());
    sheet.setDefaultColumnStyle(3, cellStyle);
    assertEquals(1, ctWorksheet.getColsArray(0).getColArray(0).getStyle());
    workbook.close();
}
Also used : CTXf(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf) CTWorksheet(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet) StylesTable(org.apache.poi.xssf.model.StylesTable) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Test(org.junit.Test)

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