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.

ExcelNumberFormat.java 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. 2012 - Alfresco Software, Ltd.
  15. Alfresco Software has modified source of this file
  16. The details of changes as svn diff can be found in svn at location root/projects/3rd-party/src
  17. ==================================================================== */
  18. package org.apache.poi.ss.usermodel;
  19. import java.util.List;
  20. import org.apache.poi.ss.formula.ConditionalFormattingEvaluator;
  21. import org.apache.poi.ss.formula.EvaluationConditionalFormatRule;
  22. /**
  23. * Object to hold a number format index and string, for various formatting evaluations
  24. */
  25. public class ExcelNumberFormat {
  26. private final int idx;
  27. private final String format;
  28. /**
  29. * @return null if the style is null, instance from style data format values otherwise
  30. */
  31. public static ExcelNumberFormat from(CellStyle style) {
  32. if (style == null) return null;
  33. return new ExcelNumberFormat(style.getDataFormat(), style.getDataFormatString());
  34. }
  35. /**
  36. * @param cell cell to extract format from
  37. * @param cfEvaluator ConditionalFormattingEvaluator to use, or null if none in this context
  38. * @return number format from highest-priority rule with a number format, or the cell style, or null if none of the above apply/are defined
  39. */
  40. public static ExcelNumberFormat from(Cell cell, ConditionalFormattingEvaluator cfEvaluator) {
  41. if (cell == null) return null;
  42. ExcelNumberFormat nf = null;
  43. if (cfEvaluator != null) {
  44. // first one wins (priority order, per Excel help)
  45. List<EvaluationConditionalFormatRule> rules = cfEvaluator.getConditionalFormattingForCell(cell);
  46. for (EvaluationConditionalFormatRule rule : rules) {
  47. nf = rule.getNumberFormat();
  48. if (nf != null) break;
  49. }
  50. }
  51. if (nf == null) {
  52. CellStyle style = cell.getCellStyle();
  53. nf = ExcelNumberFormat.from(style);
  54. }
  55. return nf;
  56. }
  57. /**
  58. * Use this carefully, prefer factory methods to ensure id/format relationships are not broken or confused.
  59. * Left public so {@link ConditionalFormattingRule#getNumberFormat()} implementations can use it.
  60. * @param idx Excel number format index, either a built-in or a higher custom # mapped in the workbook style table
  61. * @param format Excel number format string for the index
  62. */
  63. public ExcelNumberFormat(int idx, String format) {
  64. this.idx = idx;
  65. this.format = format;
  66. }
  67. /**
  68. *
  69. * @return Excel number format index, either a built-in or a higher custom # mapped in the workbook style table
  70. */
  71. public int getIdx() {
  72. return idx;
  73. }
  74. /**
  75. *
  76. * @return Excel number format string for the index
  77. */
  78. public String getFormat() {
  79. return format;
  80. }
  81. }