]> source.dussan.org Git - poi.git/commitdiff
Add another Conditional Formatting example - multiple rules which overlap
authorNick Burch <nick@apache.org>
Sat, 11 Jul 2015 20:45:58 +0000 (20:45 +0000)
committerNick Burch <nick@apache.org>
Sat, 11 Jul 2015 20:45:58 +0000 (20:45 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1690405 13f79535-47bb-0310-9956-ffa450edef68

src/examples/src/org/apache/poi/ss/examples/ConditionalFormats.java

index d0ce7674ee20a3e4825a504e63d3bb1cf74fb60b..940f5a3807cc7f07a04e70dddfc8619d47150d89 100644 (file)
@@ -46,6 +46,7 @@ public class ConditionalFormats {
 \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
@@ -60,7 +61,7 @@ public class ConditionalFormats {
         FileOutputStream out = new FileOutputStream(file);\r
         wb.write(out);\r
         out.close();\r
-\r
+        System.out.println("Generated: " + file);\r
     }\r
 \r
     /**\r
@@ -139,6 +140,71 @@ public class ConditionalFormats {
 \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