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.

CellFormat.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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.ss.format;
  16. import java.util.ArrayList;
  17. import java.util.Date;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.WeakHashMap;
  21. import java.util.logging.Level;
  22. import java.util.regex.Matcher;
  23. import java.util.regex.Pattern;
  24. import javax.swing.JLabel;
  25. import org.apache.poi.ss.usermodel.Cell;
  26. import org.apache.poi.ss.usermodel.CellType;
  27. import org.apache.poi.ss.usermodel.ConditionalFormatting;
  28. import org.apache.poi.ss.usermodel.ConditionalFormattingRule;
  29. import org.apache.poi.ss.usermodel.DataFormatter;
  30. import org.apache.poi.ss.usermodel.DateUtil;
  31. import org.apache.poi.ss.util.DateFormatConverter;
  32. /**
  33. * Format a value according to the standard Excel behavior. This "standard" is
  34. * not explicitly documented by Microsoft, so the behavior is determined by
  35. * experimentation; see the tests.
  36. * <p/>
  37. * An Excel format has up to four parts, separated by semicolons. Each part
  38. * specifies what to do with particular kinds of values, depending on the number
  39. * of parts given:
  40. * <dl>
  41. * <dt>One part (example: <tt>[Green]#.##</tt>)</dt>
  42. * <dd>If the value is a number, display according to this one part (example: green text,
  43. * with up to two decimal points). If the value is text, display it as is.</dd>
  44. *
  45. * <dt>Two parts (example: <tt>[Green]#.##;[Red]#.##</tt>)</dt>
  46. * <dd>If the value is a positive number or zero, display according to the first part (example: green
  47. * text, with up to two decimal points); if it is a negative number, display
  48. * according to the second part (example: red text, with up to two decimal
  49. * points). If the value is text, display it as is.</dd>
  50. *
  51. * <dt>Three parts (example: <tt>[Green]#.##;[Black]#.##;[Red]#.##</tt>)</dt>
  52. * <dd>If the value is a positive
  53. * number, display according to the first part (example: green text, with up to
  54. * two decimal points); if it is zero, display according to the second part
  55. * (example: black text, with up to two decimal points); if it is a negative
  56. * number, display according to the third part (example: red text, with up to
  57. * two decimal points). If the value is text, display it as is.</dd>
  58. *
  59. * <dt>Four parts (example: <tt>[Green]#.##;[Black]#.##;[Red]#.##;[@]</tt>)</dt>
  60. * <dd>If the value is a positive number, display according to the first part (example: green text,
  61. * with up to two decimal points); if it is zero, display according to the
  62. * second part (example: black text, with up to two decimal points); if it is a
  63. * negative number, display according to the third part (example: red text, with
  64. * up to two decimal points). If the value is text, display according to the
  65. * fourth part (example: text in the cell's usual color, with the text value
  66. * surround by brackets).</dd>
  67. * </dl>
  68. * <p/>
  69. * A given format part may specify a given Locale, by including something
  70. * like <tt>[$$-409]</tt> or <tt>[$&pound;-809]</tt> or <tt>[$-40C]</tt>. These
  71. * are (currently) largely ignored. You can use {@link DateFormatConverter}
  72. * to look these up into Java Locales if desired.
  73. * <p/>
  74. * In addition to these, there is a general format that is used when no format
  75. * is specified. This formatting is presented by the {@link #GENERAL_FORMAT}
  76. * object.
  77. *
  78. * TODO Merge this with {@link DataFormatter} so we only have one set of
  79. * code for formatting numbers.
  80. * TODO Re-use parts of this logic with {@link ConditionalFormatting} /
  81. * {@link ConditionalFormattingRule} for reporting stylings which do/don't apply
  82. * TODO Support the full set of modifiers, including alternate calendars and
  83. * native character numbers, as documented at https://help.libreoffice.org/Common/Number_Format_Codes
  84. */
  85. public class CellFormat {
  86. private final String format;
  87. private final CellFormatPart posNumFmt;
  88. private final CellFormatPart zeroNumFmt;
  89. private final CellFormatPart negNumFmt;
  90. private final CellFormatPart textFmt;
  91. private final int formatPartCount;
  92. private static final Pattern ONE_PART = Pattern.compile(
  93. CellFormatPart.FORMAT_PAT.pattern() + "(;|$)",
  94. Pattern.COMMENTS | Pattern.CASE_INSENSITIVE);
  95. private static final CellFormatPart DEFAULT_TEXT_FORMAT =
  96. new CellFormatPart("@");
  97. /*
  98. * Cells that cannot be formatted, e.g. cells that have a date or time
  99. * format and have an invalid date or time value, are displayed as 255
  100. * pound signs ("#").
  101. */
  102. private static final String INVALID_VALUE_FOR_FORMAT =
  103. "###################################################" +
  104. "###################################################" +
  105. "###################################################" +
  106. "###################################################" +
  107. "###################################################";
  108. private static String QUOTE = "\"";
  109. /**
  110. * Format a value as it would be were no format specified. This is also
  111. * used when the format specified is <tt>General</tt>.
  112. */
  113. public static final CellFormat GENERAL_FORMAT = new CellFormat("General") {
  114. @Override
  115. public CellFormatResult apply(Object value) {
  116. String text = (new CellGeneralFormatter()).format(value);
  117. return new CellFormatResult(true, text, null);
  118. }
  119. };
  120. /** Maps a format string to its parsed version for efficiencies sake. */
  121. private static final Map<String, CellFormat> formatCache =
  122. new WeakHashMap<String, CellFormat>();
  123. /**
  124. * Returns a {@link CellFormat} that applies the given format. Two calls
  125. * with the same format may or may not return the same object.
  126. *
  127. * @param format The format.
  128. *
  129. * @return A {@link CellFormat} that applies the given format.
  130. */
  131. public static CellFormat getInstance(String format) {
  132. CellFormat fmt = formatCache.get(format);
  133. if (fmt == null) {
  134. if (format.equals("General") || format.equals("@"))
  135. fmt = GENERAL_FORMAT;
  136. else
  137. fmt = new CellFormat(format);
  138. formatCache.put(format, fmt);
  139. }
  140. return fmt;
  141. }
  142. /**
  143. * Creates a new object.
  144. *
  145. * @param format The format.
  146. */
  147. private CellFormat(String format) {
  148. this.format = format;
  149. Matcher m = ONE_PART.matcher(format);
  150. List<CellFormatPart> parts = new ArrayList<CellFormatPart>();
  151. while (m.find()) {
  152. try {
  153. String valueDesc = m.group();
  154. // Strip out the semicolon if it's there
  155. if (valueDesc.endsWith(";"))
  156. valueDesc = valueDesc.substring(0, valueDesc.length() - 1);
  157. parts.add(new CellFormatPart(valueDesc));
  158. } catch (RuntimeException e) {
  159. CellFormatter.logger.log(Level.WARNING,
  160. "Invalid format: " + CellFormatter.quote(m.group()), e);
  161. parts.add(null);
  162. }
  163. }
  164. formatPartCount = parts.size();
  165. switch (formatPartCount) {
  166. case 1:
  167. posNumFmt = parts.get(0);
  168. negNumFmt = null;
  169. zeroNumFmt = null;
  170. textFmt = DEFAULT_TEXT_FORMAT;
  171. break;
  172. case 2:
  173. posNumFmt = parts.get(0);
  174. negNumFmt = parts.get(1);
  175. zeroNumFmt = null;
  176. textFmt = DEFAULT_TEXT_FORMAT;
  177. break;
  178. case 3:
  179. posNumFmt = parts.get(0);
  180. negNumFmt = parts.get(1);
  181. zeroNumFmt = parts.get(2);
  182. textFmt = DEFAULT_TEXT_FORMAT;
  183. break;
  184. case 4:
  185. default:
  186. posNumFmt = parts.get(0);
  187. negNumFmt = parts.get(1);
  188. zeroNumFmt = parts.get(2);
  189. textFmt = parts.get(3);
  190. break;
  191. }
  192. }
  193. /**
  194. * Returns the result of applying the format to the given value. If the
  195. * value is a number (a type of {@link Number} object), the correct number
  196. * format type is chosen; otherwise it is considered a text object.
  197. *
  198. * @param value The value
  199. *
  200. * @return The result, in a {@link CellFormatResult}.
  201. */
  202. public CellFormatResult apply(Object value) {
  203. if (value instanceof Number) {
  204. Number num = (Number) value;
  205. double val = num.doubleValue();
  206. if (val < 0 &&
  207. ((formatPartCount == 2
  208. && !posNumFmt.hasCondition() && !negNumFmt.hasCondition())
  209. || (formatPartCount == 3 && !negNumFmt.hasCondition())
  210. || (formatPartCount == 4 && !negNumFmt.hasCondition()))) {
  211. // The negative number format has the negative formatting required,
  212. // e.g. minus sign or brackets, so pass a positive value so that
  213. // the default leading minus sign is not also output
  214. return negNumFmt.apply(-val);
  215. } else {
  216. return getApplicableFormatPart(val).apply(val);
  217. }
  218. } else if (value instanceof java.util.Date) {
  219. // Don't know (and can't get) the workbook date windowing (1900 or 1904)
  220. // so assume 1900 date windowing
  221. Double numericValue = DateUtil.getExcelDate((Date) value);
  222. if (DateUtil.isValidExcelDate(numericValue)) {
  223. return getApplicableFormatPart(numericValue).apply(value);
  224. } else {
  225. throw new IllegalArgumentException("value " + numericValue + " of date " + value + " is not a valid Excel date");
  226. }
  227. } else {
  228. return textFmt.apply(value);
  229. }
  230. }
  231. /**
  232. * Returns the result of applying the format to the given date.
  233. *
  234. * @param date The date.
  235. * @param numericValue The numeric value for the date.
  236. *
  237. * @return The result, in a {@link CellFormatResult}.
  238. */
  239. private CellFormatResult apply(Date date, double numericValue) {
  240. return getApplicableFormatPart(numericValue).apply(date);
  241. }
  242. /**
  243. * Fetches the appropriate value from the cell, and returns the result of
  244. * applying it to the appropriate format. For formula cells, the computed
  245. * value is what is used.
  246. *
  247. * @param c The cell.
  248. *
  249. * @return The result, in a {@link CellFormatResult}.
  250. */
  251. public CellFormatResult apply(Cell c) {
  252. switch (ultimateTypeEnum(c)) {
  253. case BLANK:
  254. return apply("");
  255. case BOOLEAN:
  256. return apply(c.getBooleanCellValue());
  257. case NUMERIC:
  258. Double value = c.getNumericCellValue();
  259. if (getApplicableFormatPart(value).getCellFormatType() == CellFormatType.DATE) {
  260. if (DateUtil.isValidExcelDate(value)) {
  261. return apply(c.getDateCellValue(), value);
  262. } else {
  263. return apply(INVALID_VALUE_FOR_FORMAT);
  264. }
  265. } else {
  266. return apply(value);
  267. }
  268. case STRING:
  269. return apply(c.getStringCellValue());
  270. default:
  271. return apply("?");
  272. }
  273. }
  274. /**
  275. * Uses the result of applying this format to the value, setting the text
  276. * and color of a label before returning the result.
  277. *
  278. * @param label The label to apply to.
  279. * @param value The value to process.
  280. *
  281. * @return The result, in a {@link CellFormatResult}.
  282. */
  283. public CellFormatResult apply(JLabel label, Object value) {
  284. CellFormatResult result = apply(value);
  285. label.setText(result.text);
  286. if (result.textColor != null) {
  287. label.setForeground(result.textColor);
  288. }
  289. return result;
  290. }
  291. /**
  292. * Uses the result of applying this format to the given date, setting the text
  293. * and color of a label before returning the result.
  294. *
  295. * @param label The label to apply to.
  296. * @param date The date.
  297. * @param numericValue The numeric value for the date.
  298. *
  299. * @return The result, in a {@link CellFormatResult}.
  300. */
  301. private CellFormatResult apply(JLabel label, Date date, double numericValue) {
  302. CellFormatResult result = apply(date, numericValue);
  303. label.setText(result.text);
  304. if (result.textColor != null) {
  305. label.setForeground(result.textColor);
  306. }
  307. return result;
  308. }
  309. /**
  310. * Fetches the appropriate value from the cell, and uses the result, setting
  311. * the text and color of a label before returning the result.
  312. *
  313. * @param label The label to apply to.
  314. * @param c The cell.
  315. *
  316. * @return The result, in a {@link CellFormatResult}.
  317. */
  318. public CellFormatResult apply(JLabel label, Cell c) {
  319. switch (ultimateTypeEnum(c)) {
  320. case BLANK:
  321. return apply(label, "");
  322. case BOOLEAN:
  323. return apply(label, c.getBooleanCellValue());
  324. case NUMERIC:
  325. Double value = c.getNumericCellValue();
  326. if (getApplicableFormatPart(value).getCellFormatType() == CellFormatType.DATE) {
  327. if (DateUtil.isValidExcelDate(value)) {
  328. return apply(label, c.getDateCellValue(), value);
  329. } else {
  330. return apply(label, INVALID_VALUE_FOR_FORMAT);
  331. }
  332. } else {
  333. return apply(label, value);
  334. }
  335. case STRING:
  336. return apply(label, c.getStringCellValue());
  337. default:
  338. return apply(label, "?");
  339. }
  340. }
  341. /**
  342. * Returns the {@link CellFormatPart} that applies to the value. Result
  343. * depends on how many parts the cell format has, the cell value and any
  344. * conditions. The value must be a {@link Number}.
  345. *
  346. * @param value The value.
  347. * @return The {@link CellFormatPart} that applies to the value.
  348. */
  349. private CellFormatPart getApplicableFormatPart(Object value) {
  350. if (value instanceof Number) {
  351. double val = ((Number) value).doubleValue();
  352. if (formatPartCount == 1) {
  353. if (!posNumFmt.hasCondition()
  354. || (posNumFmt.hasCondition() && posNumFmt.applies(val))) {
  355. return posNumFmt;
  356. } else {
  357. return new CellFormatPart("General");
  358. }
  359. } else if (formatPartCount == 2) {
  360. if ((!posNumFmt.hasCondition() && val >= 0)
  361. || (posNumFmt.hasCondition() && posNumFmt.applies(val))) {
  362. return posNumFmt;
  363. } else if (!negNumFmt.hasCondition()
  364. || (negNumFmt.hasCondition() && negNumFmt.applies(val))) {
  365. return negNumFmt;
  366. } else {
  367. // Return ###...### (255 #s) to match Excel 2007 behaviour
  368. return new CellFormatPart(QUOTE + INVALID_VALUE_FOR_FORMAT + QUOTE);
  369. }
  370. } else {
  371. if ((!posNumFmt.hasCondition() && val > 0)
  372. || (posNumFmt.hasCondition() && posNumFmt.applies(val))) {
  373. return posNumFmt;
  374. } else if ((!negNumFmt.hasCondition() && val < 0)
  375. || (negNumFmt.hasCondition() && negNumFmt.applies(val))) {
  376. return negNumFmt;
  377. // Only the first two format parts can have conditions
  378. } else {
  379. return zeroNumFmt;
  380. }
  381. }
  382. } else {
  383. throw new IllegalArgumentException("value must be a Number");
  384. }
  385. }
  386. /**
  387. * Returns the ultimate cell type, following the results of formulas. If
  388. * the cell is a {@link CellType#FORMULA}, this returns the result of
  389. * {@link Cell#getCachedFormulaResultTypeEnum()}. Otherwise this returns the
  390. * result of {@link Cell#getCellTypeEnum()}.
  391. *
  392. * Will return {@link CellType} in a future version of POI.
  393. * For forwards compatibility, do not hard-code cell type literals in your code.
  394. *
  395. * @param cell The cell.
  396. *
  397. * @return The ultimate type of this cell.
  398. * @deprecated POI 3.15. This will return a CellType enum in the future
  399. */
  400. public static int ultimateType(Cell cell) {
  401. return ultimateTypeEnum(cell).getCode();
  402. }
  403. /**
  404. * Returns the ultimate cell type, following the results of formulas. If
  405. * the cell is a {@link CellType#FORMULA}, this returns the result of
  406. * {@link Cell#getCachedFormulaResultTypeEnum()}. Otherwise this returns the
  407. * result of {@link Cell#getCellTypeEnum()}.
  408. *
  409. * @param cell The cell.
  410. *
  411. * @return The ultimate type of this cell.
  412. * @since POI 3.15 beta 3
  413. * @deprecated POI 3.15 beta 3
  414. * Will be deleted when we make the CellType enum transition. See bug 59791.
  415. */
  416. public static CellType ultimateTypeEnum(Cell cell) {
  417. CellType type = cell.getCellTypeEnum();
  418. if (type == CellType.FORMULA)
  419. return cell.getCachedFormulaResultTypeEnum();
  420. else
  421. return type;
  422. }
  423. /**
  424. * Returns <tt>true</tt> if the other object is a {@link CellFormat} object
  425. * with the same format.
  426. *
  427. * @param obj The other object.
  428. *
  429. * @return <tt>true</tt> if the two objects are equal.
  430. */
  431. @Override
  432. public boolean equals(Object obj) {
  433. if (this == obj)
  434. return true;
  435. if (obj instanceof CellFormat) {
  436. CellFormat that = (CellFormat) obj;
  437. return format.equals(that.format);
  438. }
  439. return false;
  440. }
  441. /**
  442. * Returns a hash code for the format.
  443. *
  444. * @return A hash code for the format.
  445. */
  446. @Override
  447. public int hashCode() {
  448. return format.hashCode();
  449. }
  450. }