Search in sources :

Example 6 with PatternFormatting

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

the class ConditionalFormats method sameCell.

/**
     * Highlight cells based on their values
     */
static void sameCell(Sheet sheet) {
    sheet.createRow(0).createCell(0).setCellValue(84);
    sheet.createRow(1).createCell(0).setCellValue(74);
    sheet.createRow(2).createCell(0).setCellValue(50);
    sheet.createRow(3).createCell(0).setCellValue(51);
    sheet.createRow(4).createCell(0).setCellValue(49);
    sheet.createRow(5).createCell(0).setCellValue(41);
    SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
    // Condition 1: Cell Value Is   greater than  70   (Blue Fill)
    ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(ComparisonOperator.GT, "70");
    PatternFormatting fill1 = rule1.createPatternFormatting();
    fill1.setFillBackgroundColor(IndexedColors.BLUE.index);
    fill1.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
    // Condition 2: Cell Value Is  less than      50   (Green Fill)
    ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule(ComparisonOperator.LT, "50");
    PatternFormatting fill2 = rule2.createPatternFormatting();
    fill2.setFillBackgroundColor(IndexedColors.GREEN.index);
    fill2.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
    CellRangeAddress[] regions = { CellRangeAddress.valueOf("A1:A6") };
    sheetCF.addConditionalFormatting(regions, rule1, rule2);
    sheet.getRow(0).createCell(2).setCellValue("<== Condition 1: Cell Value Is greater than 70 (Blue Fill)");
    sheet.getRow(4).createCell(2).setCellValue("<== Condition 2: Cell Value Is less than 50 (Green Fill)");
}
Also used : PatternFormatting(org.apache.poi.ss.usermodel.PatternFormatting) SheetConditionalFormatting(org.apache.poi.ss.usermodel.SheetConditionalFormatting) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) ConditionalFormattingRule(org.apache.poi.ss.usermodel.ConditionalFormattingRule)

Example 7 with PatternFormatting

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

the class ConditionalFormats method inList.

/**
     * Use Excel conditional formatting to highlight items that are in a list on the worksheet.
     */
static void inList(Sheet sheet) {
    sheet.createRow(0).createCell(0).setCellValue("Codes");
    sheet.createRow(1).createCell(0).setCellValue("AA");
    sheet.createRow(2).createCell(0).setCellValue("BB");
    sheet.createRow(3).createCell(0).setCellValue("GG");
    sheet.createRow(4).createCell(0).setCellValue("AA");
    sheet.createRow(5).createCell(0).setCellValue("FF");
    sheet.createRow(6).createCell(0).setCellValue("XX");
    sheet.createRow(7).createCell(0).setCellValue("CC");
    sheet.getRow(0).createCell(2).setCellValue("Valid");
    sheet.getRow(1).createCell(2).setCellValue("AA");
    sheet.getRow(2).createCell(2).setCellValue("BB");
    sheet.getRow(3).createCell(2).setCellValue("CC");
    SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
    // Condition 1: Formula Is   =A2=A1   (White Font)
    ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule("COUNTIF($C$2:$C$4,A2)");
    PatternFormatting fill1 = rule1.createPatternFormatting();
    fill1.setFillBackgroundColor(IndexedColors.LIGHT_BLUE.index);
    fill1.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
    CellRangeAddress[] regions = { CellRangeAddress.valueOf("A2:A8") };
    sheetCF.addConditionalFormatting(regions, rule1);
    sheet.getRow(2).createCell(3).setCellValue("<== Use Excel conditional formatting to highlight items that are in a list on the worksheet");
}
Also used : PatternFormatting(org.apache.poi.ss.usermodel.PatternFormatting) SheetConditionalFormatting(org.apache.poi.ss.usermodel.SheetConditionalFormatting) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) ConditionalFormattingRule(org.apache.poi.ss.usermodel.ConditionalFormattingRule)

Example 8 with PatternFormatting

use of org.apache.poi.ss.usermodel.PatternFormatting 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

PatternFormatting (org.apache.poi.ss.usermodel.PatternFormatting)8 ConditionalFormattingRule (org.apache.poi.ss.usermodel.ConditionalFormattingRule)7 SheetConditionalFormatting (org.apache.poi.ss.usermodel.SheetConditionalFormatting)6 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)6 FontFormatting (org.apache.poi.ss.usermodel.FontFormatting)3 Row (org.apache.poi.ss.usermodel.Row)3 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 DifferentialStyleProvider (org.apache.poi.ss.usermodel.DifferentialStyleProvider)1 Sheet (org.apache.poi.ss.usermodel.Sheet)1 TableStyle (org.apache.poi.ss.usermodel.TableStyle)1 CellReference (org.apache.poi.ss.util.CellReference)1 Test (org.junit.Test)1