Search in sources :

Example 36 with CellStyle

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

the class FillsAndColors method main.

public static void main(String[] args) throws IOException {
    //or new HSSFWorkbook();
    Workbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet("new sheet");
    // Create a row and put some cells in it. Rows are 0 based.
    Row row = sheet.createRow(1);
    // Aqua background
    CellStyle style = wb.createCellStyle();
    style.setFillBackgroundColor(IndexedColors.AQUA.getIndex());
    style.setFillPattern(FillPatternType.BIG_SPOTS);
    Cell cell = row.createCell(1);
    cell.setCellValue(new XSSFRichTextString("X"));
    cell.setCellStyle(style);
    // Orange "foreground", foreground being the fill foreground not the font color.
    style = wb.createCellStyle();
    style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    cell = row.createCell(2);
    cell.setCellValue(new XSSFRichTextString("X"));
    cell.setCellStyle(style);
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("fill_colors.xlsx");
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}
Also used : XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) 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) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook)

Example 37 with CellStyle

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

the class TestCellStyle method test56959.

public void test56959() throws IOException {
    Workbook wb = new HSSFWorkbook();
    Sheet sheet = wb.createSheet("somesheet");
    Row row = sheet.createRow(0);
    // Create a new font and alter it.
    Font font = wb.createFont();
    font.setFontHeightInPoints((short) 24);
    font.setFontName("Courier New");
    font.setItalic(true);
    font.setStrikeout(true);
    font.setColor(Font.COLOR_RED);
    CellStyle style = wb.createCellStyle();
    style.setBorderBottom(BorderStyle.DOTTED);
    style.setFont(font);
    Cell cell = row.createCell(0);
    cell.setCellStyle(style);
    cell.setCellValue("testtext");
    Cell newCell = row.createCell(1);
    newCell.setCellStyle(style);
    newCell.setCellValue("2testtext2");
    CellStyle newStyle = newCell.getCellStyle();
    assertEquals(BorderStyle.DOTTED, newStyle.getBorderBottomEnum());
    assertEquals(Font.COLOR_RED, ((HSSFCellStyle) newStyle).getFont(wb).getColor());
//        OutputStream out = new FileOutputStream("/tmp/56959.xls");
//        try {
//            wb.write(out);
//        } finally {
//            out.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) Font(org.apache.poi.ss.usermodel.Font)

Example 38 with CellStyle

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

the class TestBugs method bug49524.

/**
     * Vertically aligned text
     */
@Test
public void bug49524() throws Exception {
    HSSFWorkbook wb1 = openSample("49524.xls");
    Sheet s = wb1.getSheetAt(0);
    Row r = s.getRow(0);
    Cell rotated = r.getCell(0);
    Cell normal = r.getCell(1);
    // Check the current ones
    assertEquals(0, normal.getCellStyle().getRotation());
    assertEquals(0xff, rotated.getCellStyle().getRotation());
    // Add a new style, also rotated
    CellStyle cs = wb1.createCellStyle();
    cs.setRotation((short) 0xff);
    Cell nc = r.createCell(2);
    nc.setCellValue("New Rotated Text");
    nc.setCellStyle(cs);
    assertEquals(0xff, nc.getCellStyle().getRotation());
    // Write out and read back
    HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
    wb1.close();
    // Re-check
    s = wb2.getSheetAt(0);
    r = s.getRow(0);
    rotated = r.getCell(0);
    normal = r.getCell(1);
    nc = r.getCell(2);
    assertEquals(0, normal.getCellStyle().getRotation());
    assertEquals(0xff, rotated.getCellStyle().getRotation());
    assertEquals(0xff, nc.getCellStyle().getRotation());
    wb2.close();
}
Also used : Row(org.apache.poi.ss.usermodel.Row) CellStyle(org.apache.poi.ss.usermodel.CellStyle) InternalSheet(org.apache.poi.hssf.model.InternalSheet) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Test(org.junit.Test)

Example 39 with CellStyle

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

the class TestBugs method bug49237.

/** Row style information is 12 not 16 bits */
@Test
public void bug49237() throws Exception {
    Workbook wb = openSample("49237.xls");
    Sheet sheet = wb.getSheetAt(0);
    Row row = sheet.getRow(0);
    CellStyle rstyle = row.getRowStyle();
    assertNotNull(rstyle);
    assertEquals(BorderStyle.DOUBLE, rstyle.getBorderBottomEnum());
    wb.close();
}
Also used : Row(org.apache.poi.ss.usermodel.Row) CellStyle(org.apache.poi.ss.usermodel.CellStyle) InternalSheet(org.apache.poi.hssf.model.InternalSheet) Sheet(org.apache.poi.ss.usermodel.Sheet) InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Example 40 with CellStyle

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

the class TestXSSFDataFormat method test58778.

/**
     * [Bug 58778] Built-in number formats can be overridden with XSSFDataFormat.putFormat(int id, String fmt)
     */
@Test
public void test58778() throws IOException {
    XSSFWorkbook wb1 = new XSSFWorkbook();
    Cell cell = wb1.createSheet("bug58778").createRow(0).createCell(0);
    cell.setCellValue(5.25);
    CellStyle style = wb1.createCellStyle();
    XSSFDataFormat dataFormat = wb1.createDataFormat();
    short poundFmtIdx = 6;
    dataFormat.putFormat(poundFmtIdx, poundFmt);
    style.setDataFormat(poundFmtIdx);
    cell.setCellStyle(style);
    // Cell should appear as "<poundsymbol>5"
    XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutCloseAndReadBack(wb1);
    cell = wb2.getSheet("bug58778").getRow(0).getCell(0);
    assertEquals(5.25, cell.getNumericCellValue(), 0);
    style = cell.getCellStyle();
    assertEquals(poundFmt, style.getDataFormatString());
    assertEquals(poundFmtIdx, style.getDataFormat());
    // manually check the file to make sure the cell is rendered as "<poundsymbol>5"
    // Verified with LibreOffice 4.2.8.2 on 2015-12-28
    wb2.close();
    wb1.close();
}
Also used : CellStyle(org.apache.poi.ss.usermodel.CellStyle) Cell(org.apache.poi.ss.usermodel.Cell) 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