]> source.dussan.org Git - poi.git/commitdiff
Add a HSSF/XSSF example relating to cell styles and colours
authorNick Burch <nick@apache.org>
Fri, 30 Sep 2011 19:30:15 +0000 (19:30 +0000)
committerNick Burch <nick@apache.org>
Fri, 30 Sep 2011 19:30:15 +0000 (19:30 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1177793 13f79535-47bb-0310-9956-ffa450edef68

src/examples/src/org/apache/poi/ss/examples/CellStyleDetails.java [new file with mode: 0644]

diff --git a/src/examples/src/org/apache/poi/ss/examples/CellStyleDetails.java b/src/examples/src/org/apache/poi/ss/examples/CellStyleDetails.java
new file mode 100644 (file)
index 0000000..8dab14f
--- /dev/null
@@ -0,0 +1,94 @@
+/* ====================================================================\r
+   Licensed to the Apache Software Foundation (ASF) under one or more\r
+   contributor license agreements.  See the NOTICE file distributed with\r
+   this work for additional information regarding copyright ownership.\r
+   The ASF licenses this file to You under the Apache License, Version 2.0\r
+   (the "License"); you may not use this file except in compliance with\r
+   the License.  You may obtain a copy of the License at\r
+\r
+       http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+==================================================================== */\r
+package org.apache.poi.ss.examples;\r
+\r
+import java.io.File;\r
+\r
+import org.apache.poi.hssf.usermodel.HSSFFont;\r
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;\r
+import org.apache.poi.hssf.util.HSSFColor;\r
+import org.apache.poi.ss.usermodel.Cell;\r
+import org.apache.poi.ss.usermodel.CellStyle;\r
+import org.apache.poi.ss.usermodel.Color;\r
+import org.apache.poi.ss.usermodel.DataFormatter;\r
+import org.apache.poi.ss.usermodel.Font;\r
+import org.apache.poi.ss.usermodel.Row;\r
+import org.apache.poi.ss.usermodel.Sheet;\r
+import org.apache.poi.ss.usermodel.Workbook;\r
+import org.apache.poi.ss.usermodel.WorkbookFactory;\r
+import org.apache.poi.ss.util.CellReference;\r
+import org.apache.poi.xssf.usermodel.XSSFColor;\r
+import org.apache.poi.xssf.usermodel.XSSFFont;\r
+\r
+/**\r
+ * Demonstrates how to read excel styles for cells\r
+ */\r
+public class CellStyleDetails {\r
+    public static void main(String[] args) throws Exception {\r
+        if(args.length == 0) {\r
+           throw new IllegalArgumentException("Filename must be given");\r
+        }\r
+        \r
+        Workbook wb = WorkbookFactory.create(new File(args[0]));\r
+        DataFormatter formatter = new DataFormatter();\r
+       \r
+        for(int sn=0; sn<wb.getNumberOfSheets(); sn++) {\r
+           Sheet sheet = wb.getSheetAt(sn);\r
+           System.out.println("Sheet #" + sn + " : " + sheet.getSheetName());\r
+           \r
+           for(Row row : sheet) {\r
+              System.out.println("  Row " + row.getRowNum());\r
+              \r
+              for(Cell cell : row) {\r
+                 CellReference ref = new CellReference(cell);\r
+                 System.out.print("    " + ref.formatAsString());\r
+                 System.out.print(" (" + cell.getColumnIndex() + ") ");\r
+                 \r
+                 CellStyle style = cell.getCellStyle();\r
+                 System.out.print("Format=" + style.getDataFormatString() + " ");\r
+                 System.out.print("FG=" + renderColor(style.getFillForegroundColorColor()) + " ");\r
+                 System.out.print("BG=" + renderColor(style.getFillBackgroundColorColor()) + " ");\r
+                 \r
+                 Font font = wb.getFontAt( style.getFontIndex() );\r
+                 System.out.print("Font=" + font.getFontName() + " ");\r
+                 System.out.print("FontColor=");\r
+                 if(font instanceof HSSFFont) {\r
+                    System.out.print( renderColor( ((HSSFFont)font).getHSSFColor((HSSFWorkbook)wb)) );\r
+                 }\r
+                 if(font instanceof XSSFFont) {\r
+                    System.out.print( renderColor( ((XSSFFont)font).getXSSFColor()) );\r
+                 }\r
+                 \r
+                 System.out.println();\r
+                 System.out.println("        " + formatter.formatCellValue(cell));\r
+              }\r
+           }\r
+           \r
+           System.out.println();\r
+        }\r
+    }\r
+    \r
+    private static String renderColor(Color color) {\r
+       if(color instanceof HSSFColor) {\r
+          return ((HSSFColor)color).getHexString();\r
+       } else if(color instanceof XSSFColor) {\r
+          return ((XSSFColor)color).getARGBHex();\r
+       } else {\r
+          return "(none)";\r
+       }\r
+    }\r
+}\r