Search in sources :

Example 6 with FontFormatting

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

the class ConditionalFormats method formatDuplicates.

/**
     * Use Excel conditional formatting to highlight duplicate entries in a column.
     */
static void formatDuplicates(Sheet sheet) {
    sheet.createRow(0).createCell(0).setCellValue("Code");
    sheet.createRow(1).createCell(0).setCellValue(4);
    sheet.createRow(2).createCell(0).setCellValue(3);
    sheet.createRow(3).createCell(0).setCellValue(6);
    sheet.createRow(4).createCell(0).setCellValue(3);
    sheet.createRow(5).createCell(0).setCellValue(5);
    sheet.createRow(6).createCell(0).setCellValue(8);
    sheet.createRow(7).createCell(0).setCellValue(0);
    sheet.createRow(8).createCell(0).setCellValue(2);
    sheet.createRow(9).createCell(0).setCellValue(8);
    sheet.createRow(10).createCell(0).setCellValue(6);
    SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
    // Condition 1: Formula Is   =A2=A1   (White Font)
    ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule("COUNTIF($A$2:$A$11,A2)>1");
    FontFormatting font = rule1.createFontFormatting();
    font.setFontStyle(false, true);
    font.setFontColorIndex(IndexedColors.BLUE.index);
    CellRangeAddress[] regions = { CellRangeAddress.valueOf("A2:A11") };
    sheetCF.addConditionalFormatting(regions, rule1);
    sheet.getRow(2).createCell(1).setCellValue("<== Duplicates numbers in the column are highlighted.  " + "Condition: Formula Is =COUNTIF($A$2:$A$11,A2)>1   (Blue Font)");
}
Also used : SheetConditionalFormatting(org.apache.poi.ss.usermodel.SheetConditionalFormatting) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) ConditionalFormattingRule(org.apache.poi.ss.usermodel.ConditionalFormattingRule) FontFormatting(org.apache.poi.ss.usermodel.FontFormatting)

Example 7 with FontFormatting

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

the class TestTableStyles method testCustomStyle.

@Test
public void testCustomStyle() throws Exception {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("tableStyle.xlsx");
    Table table = wb.getTable("Table1");
    assertNotNull("missing table", table);
    TableStyleInfo style = table.getStyle();
    assertNotNull("Missing table style info", style);
    assertNotNull("Missing table style", style.getStyle());
    assertEquals("Wrong name", "TestTableStyle", style.getName());
    assertEquals("Wrong name", "TestTableStyle", style.getStyle().getName());
    DifferentialStyleProvider firstColumn = style.getStyle().getStyle(TableStyleType.firstColumn);
    assertNotNull("no first column style", firstColumn);
    FontFormatting font = firstColumn.getFontFormatting();
    assertNotNull("no first col font", font);
    assertTrue("wrong first col bold", font.isBold());
    wb.close();
}
Also used : Table(org.apache.poi.ss.usermodel.Table) DifferentialStyleProvider(org.apache.poi.ss.usermodel.DifferentialStyleProvider) TableStyleInfo(org.apache.poi.ss.usermodel.TableStyleInfo) FontFormatting(org.apache.poi.ss.usermodel.FontFormatting) Test(org.junit.Test)

Example 8 with FontFormatting

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

the class TestTableStyles method testBuiltinStyleInit.

/**
     * Test that a built-in style is initialized properly
     */
@Test
public void testBuiltinStyleInit() {
    TableStyle style = XSSFBuiltinTableStyle.TableStyleMedium2.getStyle();
    assertNotNull("no style found for Medium2", style);
    assertNull("Should not have style info for blankRow", style.getStyle(TableStyleType.blankRow));
    DifferentialStyleProvider headerRow = style.getStyle(TableStyleType.headerRow);
    assertNotNull("no header row style", headerRow);
    FontFormatting font = headerRow.getFontFormatting();
    assertNotNull("No header row font formatting", font);
    assertTrue("header row not bold", font.isBold());
    PatternFormatting fill = headerRow.getPatternFormatting();
    assertNotNull("No header fill", fill);
    assertEquals("wrong header fill", 4, ((XSSFColor) fill.getFillBackgroundColorColor()).getTheme());
}
Also used : PatternFormatting(org.apache.poi.ss.usermodel.PatternFormatting) DifferentialStyleProvider(org.apache.poi.ss.usermodel.DifferentialStyleProvider) FontFormatting(org.apache.poi.ss.usermodel.FontFormatting) TableStyle(org.apache.poi.ss.usermodel.TableStyle) Test(org.junit.Test)

Aggregations

FontFormatting (org.apache.poi.ss.usermodel.FontFormatting)8 ConditionalFormattingRule (org.apache.poi.ss.usermodel.ConditionalFormattingRule)6 SheetConditionalFormatting (org.apache.poi.ss.usermodel.SheetConditionalFormatting)5 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)5 PatternFormatting (org.apache.poi.ss.usermodel.PatternFormatting)3 DifferentialStyleProvider (org.apache.poi.ss.usermodel.DifferentialStyleProvider)2 Row (org.apache.poi.ss.usermodel.Row)2 Test (org.junit.Test)2 ConditionalFormattingEvaluator (org.apache.poi.ss.formula.ConditionalFormattingEvaluator)1 EvaluationConditionalFormatRule (org.apache.poi.ss.formula.EvaluationConditionalFormatRule)1 WorkbookEvaluatorProvider (org.apache.poi.ss.formula.WorkbookEvaluatorProvider)1 Cell (org.apache.poi.ss.usermodel.Cell)1 CellStyle (org.apache.poi.ss.usermodel.CellStyle)1 Sheet (org.apache.poi.ss.usermodel.Sheet)1 Table (org.apache.poi.ss.usermodel.Table)1 TableStyle (org.apache.poi.ss.usermodel.TableStyle)1 TableStyleInfo (org.apache.poi.ss.usermodel.TableStyleInfo)1 CellReference (org.apache.poi.ss.util.CellReference)1