use of org.apache.poi.ss.usermodel.PatternFormatting in project poi by apache.
the class ConditionalFormats method multiCell.
/**
* Highlight multiple cells based on a formula
*/
static void multiCell(Sheet sheet) {
// header row
Row row0 = sheet.createRow(0);
row0.createCell(0).setCellValue("Units");
row0.createCell(1).setCellValue("Cost");
row0.createCell(2).setCellValue("Total");
Row row1 = sheet.createRow(1);
row1.createCell(0).setCellValue(71);
row1.createCell(1).setCellValue(29);
row1.createCell(2).setCellValue(2059);
Row row2 = sheet.createRow(2);
row2.createCell(0).setCellValue(85);
row2.createCell(1).setCellValue(29);
row2.createCell(2).setCellValue(2059);
Row row3 = sheet.createRow(3);
row3.createCell(0).setCellValue(71);
row3.createCell(1).setCellValue(29);
row3.createCell(2).setCellValue(2059);
SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
// Condition 1: Formula Is =$B2>75 (Blue Fill)
ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule("$A2>75");
PatternFormatting fill1 = rule1.createPatternFormatting();
fill1.setFillBackgroundColor(IndexedColors.BLUE.index);
fill1.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
CellRangeAddress[] regions = { CellRangeAddress.valueOf("A2:C4") };
sheetCF.addConditionalFormatting(regions, rule1);
sheet.getRow(2).createCell(4).setCellValue("<== Condition 1: Formula Is =$B2>75 (Blue Fill)");
}
use of org.apache.poi.ss.usermodel.PatternFormatting in project poi by apache.
the class ConditionalFormats method evaluateRules.
/**
* Print out a summary of the conditional formatting rules applied to cells on the given sheet.
* Only cells with a matching rule are printed, and for those, all matching rules are sumarized.
* @param wb
* @param sheetName
*/
static void evaluateRules(Workbook wb, String sheetName) {
final WorkbookEvaluatorProvider wbEvalProv = (WorkbookEvaluatorProvider) wb.getCreationHelper().createFormulaEvaluator();
final ConditionalFormattingEvaluator cfEval = new ConditionalFormattingEvaluator(wb, wbEvalProv);
// if cell values have changed, clear cached format results
cfEval.clearAllCachedValues();
final Sheet sheet = wb.getSheet(sheetName);
for (Row r : sheet) {
for (Cell c : r) {
final List<EvaluationConditionalFormatRule> rules = cfEval.getConditionalFormattingForCell(c);
// check rules list for null, although current implementation will return an empty list, not null, then do what you want with results
if (rules == null || rules.isEmpty())
continue;
final CellReference ref = ConditionalFormattingEvaluator.getRef(c);
if (rules.isEmpty())
continue;
System.out.println("\n" + ref.formatAsString() + " has conditional formatting.");
for (EvaluationConditionalFormatRule rule : rules) {
ConditionalFormattingRule cf = rule.getRule();
StringBuilder b = new StringBuilder();
b.append("\tRule ").append(rule.getFormattingIndex()).append(": ");
// check for color scale
if (cf.getColorScaleFormatting() != null) {
b.append("\n\t\tcolor scale (caller must calculate bucket)");
}
// check for data bar
if (cf.getDataBarFormatting() != null) {
b.append("\n\t\tdata bar (caller must calculate bucket)");
}
// check for icon set
if (cf.getMultiStateFormatting() != null) {
b.append("\n\t\ticon set (caller must calculate icon bucket)");
}
// check for fill
if (cf.getPatternFormatting() != null) {
final PatternFormatting fill = cf.getPatternFormatting();
b.append("\n\t\tfill pattern ").append(fill.getFillPattern()).append(" color index ").append(fill.getFillBackgroundColor());
}
// font stuff
if (cf.getFontFormatting() != null) {
final FontFormatting ff = cf.getFontFormatting();
b.append("\n\t\tfont format ").append("color index ").append(ff.getFontColorIndex());
if (ff.isBold())
b.append(" bold");
if (ff.isItalic())
b.append(" italic");
if (ff.isStruckout())
b.append(" strikeout");
b.append(" underline index ").append(ff.getUnderlineType());
}
System.out.println(b);
}
}
}
}
use of org.apache.poi.ss.usermodel.PatternFormatting in project poi by apache.
the class ConditionalFormats method shadeAlt.
/**
* Use Excel conditional formatting to shade alternating rows on the worksheet
*/
static void shadeAlt(Sheet sheet) {
SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
// Condition 1: Formula Is =A2=A1 (White Font)
ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule("MOD(ROW(),2)");
PatternFormatting fill1 = rule1.createPatternFormatting();
fill1.setFillBackgroundColor(IndexedColors.LIGHT_GREEN.index);
fill1.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
CellRangeAddress[] regions = { CellRangeAddress.valueOf("A1:Z100") };
sheetCF.addConditionalFormatting(regions, rule1);
sheet.createRow(0).createCell(1).setCellValue("Shade Alternating Rows");
sheet.createRow(1).createCell(1).setCellValue("Condition: Formula Is =MOD(ROW(),2) (Light Green Fill)");
}
use of org.apache.poi.ss.usermodel.PatternFormatting in project poi by apache.
the class ConditionalFormats method overlapping.
/**
* Multiple conditional formatting rules can apply to
* one cell, some combining, some beating others.
* Done in order of the rules added to the
* SheetConditionalFormatting object
*/
static void overlapping(Sheet sheet) {
for (int i = 0; i < 40; i++) {
int rn = i + 1;
Row r = sheet.createRow(i);
r.createCell(0).setCellValue("This is row " + rn + " (" + i + ")");
String str = "";
if (rn % 2 == 0) {
str = str + "even ";
}
if (rn % 3 == 0) {
str = str + "x3 ";
}
if (rn % 5 == 0) {
str = str + "x5 ";
}
if (rn % 10 == 0) {
str = str + "x10 ";
}
if (str.length() == 0) {
str = "nothing special...";
}
r.createCell(1).setCellValue("It is " + str);
}
sheet.autoSizeColumn(0);
sheet.autoSizeColumn(1);
sheet.getRow(1).createCell(3).setCellValue("Even rows are blue");
sheet.getRow(2).createCell(3).setCellValue("Multiples of 3 have a grey background");
sheet.getRow(4).createCell(3).setCellValue("Multiples of 5 are bold");
sheet.getRow(9).createCell(3).setCellValue("Multiples of 10 are red (beats even)");
SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
// Condition 1: Row divides by 10, red (will beat #1)
ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule("MOD(ROW(),10)=0");
FontFormatting font1 = rule1.createFontFormatting();
font1.setFontColorIndex(IndexedColors.RED.index);
// Condition 2: Row is even, blue
ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule("MOD(ROW(),2)=0");
FontFormatting font2 = rule2.createFontFormatting();
font2.setFontColorIndex(IndexedColors.BLUE.index);
// Condition 3: Row divides by 5, bold
ConditionalFormattingRule rule3 = sheetCF.createConditionalFormattingRule("MOD(ROW(),5)=0");
FontFormatting font3 = rule3.createFontFormatting();
font3.setFontStyle(false, true);
// Condition 4: Row divides by 3, grey background
ConditionalFormattingRule rule4 = sheetCF.createConditionalFormattingRule("MOD(ROW(),3)=0");
PatternFormatting fill4 = rule4.createPatternFormatting();
fill4.setFillBackgroundColor(IndexedColors.GREY_25_PERCENT.index);
fill4.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
// Apply
CellRangeAddress[] regions = { CellRangeAddress.valueOf("A1:F41") };
sheetCF.addConditionalFormatting(regions, rule1);
sheetCF.addConditionalFormatting(regions, rule2);
sheetCF.addConditionalFormatting(regions, rule3);
sheetCF.addConditionalFormatting(regions, rule4);
}
use of org.apache.poi.ss.usermodel.PatternFormatting in project poi by apache.
the class ConditionalFormats method shadeBands.
/**
* You can use Excel conditional formatting to shade bands of rows on the worksheet.
* In this example, 3 rows are shaded light grey, and 3 are left with no shading.
* In the MOD function, the total number of rows in the set of banded rows (6) is entered.
*/
static void shadeBands(Sheet sheet) {
SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule("MOD(ROW(),6)<3");
PatternFormatting fill1 = rule1.createPatternFormatting();
fill1.setFillBackgroundColor(IndexedColors.GREY_25_PERCENT.index);
fill1.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
CellRangeAddress[] regions = { CellRangeAddress.valueOf("A1:Z100") };
sheetCF.addConditionalFormatting(regions, rule1);
sheet.createRow(0).createCell(1).setCellValue("Shade Bands of Rows");
sheet.createRow(1).createCell(1).setCellValue("Condition: Formula Is =MOD(ROW(),6)<2 (Light Grey Fill)");
}
Aggregations