Search in sources :

Example 16 with CellStyle

use of org.apache.poi.ss.usermodel.CellStyle in project poi by apache.

the class WorkingWithFonts method main.

public static void main(String[] args) throws IOException {
    //or new HSSFWorkbook();
    Workbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet("Fonts");
    Font font0 = wb.createFont();
    font0.setColor(IndexedColors.BROWN.getIndex());
    CellStyle style0 = wb.createCellStyle();
    style0.setFont(font0);
    Font font1 = wb.createFont();
    font1.setFontHeightInPoints((short) 14);
    font1.setFontName("Courier New");
    font1.setColor(IndexedColors.RED.getIndex());
    CellStyle style1 = wb.createCellStyle();
    style1.setFont(font1);
    Font font2 = wb.createFont();
    font2.setFontHeightInPoints((short) 16);
    font2.setFontName("Arial");
    font2.setColor(IndexedColors.GREEN.getIndex());
    CellStyle style2 = wb.createCellStyle();
    style2.setFont(font2);
    Font font3 = wb.createFont();
    font3.setFontHeightInPoints((short) 18);
    font3.setFontName("Times New Roman");
    font3.setColor(IndexedColors.LAVENDER.getIndex());
    CellStyle style3 = wb.createCellStyle();
    style3.setFont(font3);
    Font font4 = wb.createFont();
    font4.setFontHeightInPoints((short) 18);
    font4.setFontName("Wingdings");
    font4.setColor(IndexedColors.GOLD.getIndex());
    CellStyle style4 = wb.createCellStyle();
    style4.setFont(font4);
    Font font5 = wb.createFont();
    font5.setFontName("Symbol");
    CellStyle style5 = wb.createCellStyle();
    style5.setFont(font5);
    Cell cell0 = sheet.createRow(0).createCell(1);
    cell0.setCellValue("Default");
    cell0.setCellStyle(style0);
    Cell cell1 = sheet.createRow(1).createCell(1);
    cell1.setCellValue("Courier");
    cell1.setCellStyle(style1);
    Cell cell2 = sheet.createRow(2).createCell(1);
    cell2.setCellValue("Arial");
    cell2.setCellStyle(style2);
    Cell cell3 = sheet.createRow(3).createCell(1);
    cell3.setCellValue("Times New Roman");
    cell3.setCellStyle(style3);
    Cell cell4 = sheet.createRow(4).createCell(1);
    cell4.setCellValue("Wingdings");
    cell4.setCellStyle(style4);
    Cell cell5 = sheet.createRow(5).createCell(1);
    cell5.setCellValue("Symbol");
    cell5.setCellStyle(style5);
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("xssf-fonts.xlsx");
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Font(org.apache.poi.ss.usermodel.Font)

Example 17 with CellStyle

use of org.apache.poi.ss.usermodel.CellStyle in project poi by apache.

the class TestStylesTable method removeNumberFormat.

@Test
public void removeNumberFormat() throws IOException {
    XSSFWorkbook wb1 = new XSSFWorkbook();
    try {
        final String fmt = customDataFormat;
        final short fmtIdx = (short) wb1.getStylesSource().putNumberFormat(fmt);
        Cell cell = wb1.createSheet("test").createRow(0).createCell(0);
        cell.setCellValue(5.25);
        CellStyle style = wb1.createCellStyle();
        style.setDataFormat(fmtIdx);
        cell.setCellStyle(style);
        assertEquals(fmt, cell.getCellStyle().getDataFormatString());
        assertEquals(fmt, wb1.getStylesSource().getNumberFormatAt(fmtIdx));
        // remove the number format from the workbook
        wb1.getStylesSource().removeNumberFormat(fmt);
        // number format in CellStyles should be restored to default number format
        final short defaultFmtIdx = 0;
        final String defaultFmt = BuiltinFormats.getBuiltinFormat(0);
        assertEquals(defaultFmtIdx, style.getDataFormat());
        assertEquals(defaultFmt, style.getDataFormatString());
        // The custom number format should be entirely removed from the workbook
        Map<Short, String> numberFormats = wb1.getStylesSource().getNumberFormats();
        assertNotContainsKey(numberFormats, fmtIdx);
        assertNotContainsValue(numberFormats, fmt);
        // The default style shouldn't be added back to the styles source because it's built-in
        assertEquals(0, wb1.getStylesSource().getNumDataFormats());
        cell = null;
        style = null;
        numberFormats = null;
        XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutCloseAndReadBack(wb1);
        cell = wb2.getSheet("test").getRow(0).getCell(0);
        style = cell.getCellStyle();
        // number format in CellStyles should be restored to default number format
        assertEquals(defaultFmtIdx, style.getDataFormat());
        assertEquals(defaultFmt, style.getDataFormatString());
        // The custom number format should be entirely removed from the workbook
        numberFormats = wb2.getStylesSource().getNumberFormats();
        assertNotContainsKey(numberFormats, fmtIdx);
        assertNotContainsValue(numberFormats, fmt);
        // The default style shouldn't be added back to the styles source because it's built-in
        assertEquals(0, wb2.getStylesSource().getNumDataFormats());
        wb2.close();
    } finally {
        wb1.close();
    }
}
Also used : XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCellStyle(org.apache.poi.xssf.usermodel.XSSFCellStyle) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Cell(org.apache.poi.ss.usermodel.Cell) Test(org.junit.Test)

Example 18 with CellStyle

use of org.apache.poi.ss.usermodel.CellStyle in project poi by apache.

the class BaseTestCellUtil method setFillForegroundColorBeforeFillBackgroundColorEnum.

/**
     * bug 55555
     * @since POI 3.15 beta 3
     */
@Test
public void setFillForegroundColorBeforeFillBackgroundColorEnum() throws IOException {
    Workbook wb1 = _testDataProvider.createWorkbook();
    Cell A1 = wb1.createSheet().createRow(0).createCell(0);
    Map<String, Object> properties = new HashMap<String, Object>();
    // FIXME: Use FillPatternType.BRICKS enum
    properties.put(CellUtil.FILL_PATTERN, FillPatternType.BRICKS);
    properties.put(CellUtil.FILL_FOREGROUND_COLOR, IndexedColors.BLUE.index);
    properties.put(CellUtil.FILL_BACKGROUND_COLOR, IndexedColors.RED.index);
    CellUtil.setCellStyleProperties(A1, properties);
    CellStyle style = A1.getCellStyle();
    // FIXME: Use FillPatternType.BRICKS enum
    assertEquals("fill pattern", FillPatternType.BRICKS, style.getFillPatternEnum());
    assertEquals("fill foreground color", IndexedColors.BLUE, IndexedColors.fromInt(style.getFillForegroundColor()));
    assertEquals("fill background color", IndexedColors.RED, IndexedColors.fromInt(style.getFillBackgroundColor()));
    wb1.close();
}
Also used : HashMap(java.util.HashMap) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Cell(org.apache.poi.ss.usermodel.Cell) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Example 19 with CellStyle

use of org.apache.poi.ss.usermodel.CellStyle in project poi by apache.

the class BaseTestCellUtil method setFillForegroundColorBeforeFillBackgroundColor.

/**
     * bug 55555
     * @deprecated Replaced by {@link #setFillForegroundColorBeforeFillBackgroundColorEnum()}
     * @since POI 3.15 beta 3
     */
@Deprecated
// bug 55555
@Test
public void setFillForegroundColorBeforeFillBackgroundColor() throws IOException {
    Workbook wb1 = _testDataProvider.createWorkbook();
    Cell A1 = wb1.createSheet().createRow(0).createCell(0);
    Map<String, Object> properties = new HashMap<String, Object>();
    // FIXME: Use FillPatternType.BRICKS enum
    properties.put(CellUtil.FILL_PATTERN, CellStyle.BRICKS);
    properties.put(CellUtil.FILL_FOREGROUND_COLOR, IndexedColors.BLUE.index);
    properties.put(CellUtil.FILL_BACKGROUND_COLOR, IndexedColors.RED.index);
    CellUtil.setCellStyleProperties(A1, properties);
    CellStyle style = A1.getCellStyle();
    // FIXME: Use FillPatternType.BRICKS enum
    assertEquals("fill pattern", CellStyle.BRICKS, style.getFillPattern());
    assertEquals("fill foreground color", IndexedColors.BLUE, IndexedColors.fromInt(style.getFillForegroundColor()));
    assertEquals("fill background color", IndexedColors.RED, IndexedColors.fromInt(style.getFillBackgroundColor()));
    wb1.close();
}
Also used : HashMap(java.util.HashMap) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Cell(org.apache.poi.ss.usermodel.Cell) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Example 20 with CellStyle

use of org.apache.poi.ss.usermodel.CellStyle in project poi by apache.

the class BaseTestCellUtil method createCell.

@Test
public void createCell() throws IOException {
    Workbook wb = _testDataProvider.createWorkbook();
    Sheet sh = wb.createSheet();
    Row row = sh.createRow(0);
    CellStyle style = wb.createCellStyle();
    style.setWrapText(true);
    // calling createCell on a non-existing cell should create a cell and set the cell value and style.
    Cell F1 = CellUtil.createCell(row, 5, "Cell Value", style);
    assertSame(row.getCell(5), F1);
    assertEquals("Cell Value", F1.getStringCellValue());
    assertEquals(style, F1.getCellStyle());
    // should be assertSame, but a new HSSFCellStyle is returned for each getCellStyle() call.
    // HSSFCellStyle wraps an underlying style record, and the underlying
    // style record is the same between multiple getCellStyle() calls.
    // calling createCell on an existing cell should return the existing cell and modify the cell value and style.
    Cell f1 = CellUtil.createCell(row, 5, "Overwritten cell value", null);
    assertSame(row.getCell(5), f1);
    assertSame(F1, f1);
    assertEquals("Overwritten cell value", f1.getStringCellValue());
    assertEquals("Overwritten cell value", F1.getStringCellValue());
    assertEquals("cell style should be unchanged with createCell(..., null)", style, f1.getCellStyle());
    assertEquals("cell style should be unchanged with createCell(..., null)", style, F1.getCellStyle());
    // test createCell(row, column, value) (no CellStyle)
    f1 = CellUtil.createCell(row, 5, "Overwritten cell with default style");
    assertSame(F1, f1);
    wb.close();
}
Also used : Row(org.apache.poi.ss.usermodel.Row) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Aggregations

CellStyle (org.apache.poi.ss.usermodel.CellStyle)53 Cell (org.apache.poi.ss.usermodel.Cell)31 Row (org.apache.poi.ss.usermodel.Row)27 Sheet (org.apache.poi.ss.usermodel.Sheet)26 Workbook (org.apache.poi.ss.usermodel.Workbook)25 Test (org.junit.Test)16 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)14 Font (org.apache.poi.ss.usermodel.Font)13 FileOutputStream (java.io.FileOutputStream)8 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)7 CreationHelper (org.apache.poi.ss.usermodel.CreationHelper)7 CellReference (org.apache.poi.ss.util.CellReference)5 Date (java.util.Date)4 DataFormat (org.apache.poi.ss.usermodel.DataFormat)4 IOException (java.io.IOException)3 DecimalFormat (java.text.DecimalFormat)3 HashMap (java.util.HashMap)3 CellType (org.apache.poi.ss.usermodel.CellType)3 File (java.io.File)2 OutputStream (java.io.OutputStream)2