\r
sameCell(wb.createSheet("Same Cell"));\r
multiCell(wb.createSheet("MultiCell"));\r
+ overlapping(wb.createSheet("Overlapping"));\r
errors(wb.createSheet("Errors"));\r
hideDupplicates(wb.createSheet("Hide Dups"));\r
formatDuplicates(wb.createSheet("Duplicates"));\r
FileOutputStream out = new FileOutputStream(file);\r
wb.write(out);\r
out.close();\r
-\r
+ System.out.println("Generated: " + file);\r
}\r
\r
/**\r
\r
sheet.getRow(2).createCell(4).setCellValue("<== Condition 1: Formula Is =$B2>75 (Blue Fill)");\r
}\r
+ \r
+ /**\r
+ * Multiple conditional formatting rules can apply to\r
+ * one cell, some combining, some beating others.\r
+ * Done in order of the rules added to the \r
+ * SheetConditionalFormatting object\r
+ */\r
+ static void overlapping(Sheet sheet) {\r
+ for (int i=0; i<40; i++) {\r
+ int rn = i+1;\r
+ Row r = sheet.createRow(i);\r
+ r.createCell(0).setCellValue("This is row " + rn + " (" + i + ")");\r
+ String str = "";\r
+ if (rn%2 == 0) str = str + "even ";\r
+ if (rn%3 == 0) str = str + "x3 ";\r
+ if (rn%5 == 0) str = str + "x5 ";\r
+ if (rn%10 == 0) str = str + "x10 ";\r
+ if (str.length() == 0) str = "nothing special...";\r
+ r.createCell(1).setCellValue("It is " + str);\r
+ }\r
+ sheet.autoSizeColumn(0);\r
+ sheet.autoSizeColumn(1);\r
+ \r
+ sheet.getRow(1).createCell(3).setCellValue("Even rows are blue");\r
+ sheet.getRow(2).createCell(3).setCellValue("Multiples of 3 have a grey background");\r
+ sheet.getRow(4).createCell(3).setCellValue("Multiples of 5 are bold");\r
+ sheet.getRow(9).createCell(3).setCellValue("Multiples of 10 are red (beats even)");\r
+ \r
+ SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();\r
+ \r
+ // Condition 1: Row divides by 10, red (will beat #1)\r
+ ConditionalFormattingRule rule1 = \r
+ sheetCF.createConditionalFormattingRule("MOD(ROW(),10)=0");\r
+ FontFormatting font1 = rule1.createFontFormatting();\r
+ font1.setFontColorIndex(IndexedColors.RED.index);\r
+ \r
+ // Condition 2: Row is even, blue\r
+ ConditionalFormattingRule rule2 = \r
+ sheetCF.createConditionalFormattingRule("MOD(ROW(),2)=0");\r
+ FontFormatting font2 = rule2.createFontFormatting();\r
+ font2.setFontColorIndex(IndexedColors.BLUE.index);\r
+ \r
+ // Condition 3: Row divides by 5, bold\r
+ ConditionalFormattingRule rule3 = \r
+ sheetCF.createConditionalFormattingRule("MOD(ROW(),5)=0");\r
+ FontFormatting font3 = rule3.createFontFormatting();\r
+ font3.setFontStyle(false, true);\r
+ \r
+ // Condition 4: Row divides by 3, grey background\r
+ ConditionalFormattingRule rule4 = \r
+ sheetCF.createConditionalFormattingRule("MOD(ROW(),3)=0");\r
+ PatternFormatting fill4 = rule4.createPatternFormatting();\r
+ fill4.setFillBackgroundColor(IndexedColors.GREY_25_PERCENT.index);\r
+ fill4.setFillPattern(PatternFormatting.SOLID_FOREGROUND);\r
+ \r
+ // Apply\r
+ CellRangeAddress[] regions = {\r
+ CellRangeAddress.valueOf("A1:F41")\r
+ };\r
+\r
+ sheetCF.addConditionalFormatting(regions, rule1);\r
+ sheetCF.addConditionalFormatting(regions, rule2);\r
+ sheetCF.addConditionalFormatting(regions, rule3);\r
+ sheetCF.addConditionalFormatting(regions, rule4);\r
+ }\r
\r
/**\r
* Use Excel conditional formatting to check for errors,\r