--- /dev/null
+/* ====================================================================\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