You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CellStyleDetails.java 4.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.examples.ss;
  16. import java.io.File;
  17. import org.apache.poi.hssf.usermodel.HSSFFont;
  18. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  19. import org.apache.poi.hssf.util.HSSFColor;
  20. import org.apache.poi.ss.usermodel.Cell;
  21. import org.apache.poi.ss.usermodel.CellStyle;
  22. import org.apache.poi.ss.usermodel.Color;
  23. import org.apache.poi.ss.usermodel.DataFormatter;
  24. import org.apache.poi.ss.usermodel.Font;
  25. import org.apache.poi.ss.usermodel.Row;
  26. import org.apache.poi.ss.usermodel.Sheet;
  27. import org.apache.poi.ss.usermodel.Workbook;
  28. import org.apache.poi.ss.usermodel.WorkbookFactory;
  29. import org.apache.poi.ss.util.CellReference;
  30. import org.apache.poi.xssf.usermodel.XSSFColor;
  31. import org.apache.poi.xssf.usermodel.XSSFFont;
  32. /**
  33. * Demonstrates how to read excel styles for cells
  34. */
  35. @SuppressWarnings({"java:S106","java:S4823"})
  36. public final class CellStyleDetails {
  37. private CellStyleDetails() {}
  38. public static void main(String[] args) throws Exception {
  39. if(args.length == 0) {
  40. throw new IllegalArgumentException("Filename must be given");
  41. }
  42. try (Workbook wb = WorkbookFactory.create(new File(args[0]))) {
  43. DataFormatter formatter = new DataFormatter();
  44. for (int sn = 0; sn < wb.getNumberOfSheets(); sn++) {
  45. Sheet sheet = wb.getSheetAt(sn);
  46. System.out.println("Sheet #" + sn + " : " + sheet.getSheetName());
  47. for (Row row : sheet) {
  48. System.out.println(" Row " + row.getRowNum());
  49. for (Cell cell : row) {
  50. CellReference ref = new CellReference(cell);
  51. System.out.print(" " + ref.formatAsString());
  52. System.out.print(" (" + cell.getColumnIndex() + ") ");
  53. CellStyle style = cell.getCellStyle();
  54. System.out.print("Format=" + style.getDataFormatString() + " ");
  55. System.out.print("FG=" + renderColor(style.getFillForegroundColorColor()) + " ");
  56. System.out.print("BG=" + renderColor(style.getFillBackgroundColorColor()) + " ");
  57. Font font = wb.getFontAt(style.getFontIndex());
  58. System.out.print("Font=" + font.getFontName() + " ");
  59. System.out.print("FontColor=");
  60. if (font instanceof HSSFFont) {
  61. System.out.print(renderColor(((HSSFFont) font).getHSSFColor((HSSFWorkbook) wb)));
  62. }
  63. if (font instanceof XSSFFont) {
  64. System.out.print(renderColor(((XSSFFont) font).getXSSFColor()));
  65. }
  66. System.out.println();
  67. System.out.println(" " + formatter.formatCellValue(cell));
  68. }
  69. }
  70. System.out.println();
  71. }
  72. }
  73. }
  74. private static String renderColor(Color color) {
  75. if(color instanceof HSSFColor) {
  76. return ((HSSFColor)color).getHexString();
  77. } else if(color instanceof XSSFColor) {
  78. return ((XSSFColor)color).getARGBHex();
  79. } else {
  80. return "(none)";
  81. }
  82. }
  83. }