Search in sources :

Example 6 with StylesTable

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

the class TestXSSFWorkbook method getFontAt.

@Test
public void getFontAt() throws IOException {
    XSSFWorkbook workbook = new XSSFWorkbook();
    try {
        StylesTable styleSource = workbook.getStylesSource();
        short i = 0;
        //get default font
        Font fontAt = workbook.getFontAt(i);
        assertNotNull(fontAt);
        //get customized font
        XSSFFont customFont = new XSSFFont();
        customFont.setItalic(true);
        int x = styleSource.putFont(customFont);
        fontAt = workbook.getFontAt((short) x);
        assertNotNull(fontAt);
    } finally {
        workbook.close();
    }
}
Also used : StylesTable(org.apache.poi.xssf.model.StylesTable) Font(org.apache.poi.ss.usermodel.Font) Test(org.junit.Test)

Example 7 with StylesTable

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

the class TestColumnHelper method testGetSetColDefaultStyle.

@Test
public void testGetSetColDefaultStyle() throws IOException {
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet();
    CTWorksheet ctWorksheet = sheet.getCTWorksheet();
    ColumnHelper columnHelper = sheet.getColumnHelper();
    // POI column 3, OOXML column 4
    CTCol col = columnHelper.getOrCreateColumn1Based(4, false);
    assertNotNull(col);
    assertNotNull(columnHelper.getColumn(3, false));
    columnHelper.setColDefaultStyle(3, 2);
    assertEquals(2, columnHelper.getColDefaultStyle(3));
    assertEquals(-1, columnHelper.getColDefaultStyle(4));
    StylesTable stylesTable = workbook.getStylesSource();
    CTXf cellXf = CTXf.Factory.newInstance();
    cellXf.setFontId(0);
    cellXf.setFillId(0);
    cellXf.setBorderId(0);
    cellXf.setNumFmtId(0);
    cellXf.setXfId(0);
    stylesTable.putCellXf(cellXf);
    CTCol col_2 = ctWorksheet.getColsArray(0).addNewCol();
    col_2.setMin(10);
    col_2.setMax(12);
    col_2.setStyle(1);
    assertEquals(1, columnHelper.getColDefaultStyle(11));
    XSSFCellStyle cellStyle = new XSSFCellStyle(0, 0, stylesTable, null);
    columnHelper.setColDefaultStyle(11, cellStyle);
    assertEquals(0, col_2.getStyle());
    assertEquals(1, columnHelper.getColDefaultStyle(10));
    workbook.close();
}
Also used : CTXf(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFCellStyle(org.apache.poi.xssf.usermodel.XSSFCellStyle) CTWorksheet(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet) CTCol(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol) StylesTable(org.apache.poi.xssf.model.StylesTable) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Test(org.junit.Test)

Example 8 with StylesTable

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

the class TestXSSFReader method test58747.

/**
    * NPE from XSSFReader$SheetIterator.<init> on XLSX files generated by
    *  the openpyxl library
    */
public void test58747() throws Exception {
    OPCPackage pkg = XSSFTestDataSamples.openSamplePackage("58747.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();
    assertEquals(true, iter.hasNext());
    iter.next();
    assertEquals(false, iter.hasNext());
    assertEquals("Orders", iter.getSheetName());
    pkg.close();
}
Also used : StylesTable(org.apache.poi.xssf.model.StylesTable) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Example 9 with StylesTable

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

the class TestXSSFCellStyle method testGetFillForegroundColor.

@Test
public void testGetFillForegroundColor() throws IOException {
    XSSFWorkbook wb = new XSSFWorkbook();
    StylesTable styles = wb.getStylesSource();
    assertEquals(1, wb.getNumCellStyles());
    assertEquals(2, styles.getFills().size());
    XSSFCellStyle defaultStyle = wb.getCellStyleAt((short) 0);
    assertEquals(IndexedColors.AUTOMATIC.getIndex(), defaultStyle.getFillForegroundColor());
    assertEquals(null, defaultStyle.getFillForegroundXSSFColor());
    assertEquals(FillPatternType.NO_FILL, defaultStyle.getFillPatternEnum());
    assertEquals(CellStyle.NO_FILL, defaultStyle.getFillPattern());
    XSSFCellStyle customStyle = wb.createCellStyle();
    customStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    assertEquals(FillPatternType.SOLID_FOREGROUND, customStyle.getFillPatternEnum());
    assertEquals(CellStyle.SOLID_FOREGROUND, customStyle.getFillPattern());
    assertEquals(3, styles.getFills().size());
    customStyle.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
    assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), customStyle.getFillForegroundColor());
    assertEquals(4, styles.getFills().size());
    for (int i = 0; i < 3; i++) {
        XSSFCellStyle style = wb.createCellStyle();
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        assertEquals(FillPatternType.SOLID_FOREGROUND, style.getFillPatternEnum());
        assertEquals(CellStyle.SOLID_FOREGROUND, style.getFillPattern());
        assertEquals(4, styles.getFills().size());
        style.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
        assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), style.getFillForegroundColor());
        assertEquals(4, styles.getFills().size());
    }
    assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
    wb.close();
}
Also used : StylesTable(org.apache.poi.xssf.model.StylesTable) Test(org.junit.Test)

Example 10 with StylesTable

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

the class TestXSSFCellStyle method testBug52348.

/**
     * Avoid ArrayIndexOutOfBoundsException  when creating cell style
     * in a workbook that has an empty xf table.
     */
@Test
public void testBug52348() throws IOException {
    XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("52348.xlsx");
    StylesTable st = workbook.getStylesSource();
    assertEquals(0, st._getStyleXfsSize());
    // no exception at this point
    XSSFCellStyle style = workbook.createCellStyle();
    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)

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