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.

DataFormatter.java 39KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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.lang.reflect.InvocationTargetException;
  20. import java.lang.reflect.Method;
  21. import java.math.RoundingMode;
  22. import java.text.DateFormatSymbols;
  23. import java.text.DecimalFormat;
  24. import java.text.DecimalFormatSymbols;
  25. import java.text.FieldPosition;
  26. import java.text.Format;
  27. import java.text.ParsePosition;
  28. import java.text.SimpleDateFormat;
  29. import java.util.ArrayList;
  30. import java.util.Date;
  31. import java.util.HashMap;
  32. import java.util.Iterator;
  33. import java.util.List;
  34. import java.util.Locale;
  35. import java.util.Map;
  36. import java.util.regex.Matcher;
  37. import java.util.regex.Pattern;
  38. /**
  39. * DataFormatter contains methods for formatting the value stored in an
  40. * Cell. This can be useful for reports and GUI presentations when you
  41. * need to display data exactly as it appears in Excel. Supported formats
  42. * include currency, SSN, percentages, decimals, dates, phone numbers, zip
  43. * codes, etc.
  44. * <p>
  45. * Internally, formats will be implemented using subclasses of {@link Format}
  46. * such as {@link DecimalFormat} and {@link SimpleDateFormat}. Therefore the
  47. * formats used by this class must obey the same pattern rules as these Format
  48. * subclasses. This means that only legal number pattern characters ("0", "#",
  49. * ".", "," etc.) may appear in number formats. Other characters can be
  50. * inserted <em>before</em> or <em> after</em> the number pattern to form a
  51. * prefix or suffix.
  52. * </p>
  53. * <p>
  54. * For example the Excel pattern <code>"$#,##0.00 "USD"_);($#,##0.00 "USD")"
  55. * </code> will be correctly formatted as "$1,000.00 USD" or "($1,000.00 USD)".
  56. * However the pattern <code>"00-00-00"</code> is incorrectly formatted by
  57. * DecimalFormat as "000000--". For Excel formats that are not compatible with
  58. * DecimalFormat, you can provide your own custom {@link Format} implementation
  59. * via <code>DataFormatter.addFormat(String,Format)</code>. The following
  60. * custom formats are already provided by this class:
  61. * </p>
  62. * <pre>
  63. * <ul><li>SSN "000-00-0000"</li>
  64. * <li>Phone Number "(###) ###-####"</li>
  65. * <li>Zip plus 4 "00000-0000"</li>
  66. * </ul>
  67. * </pre>
  68. * <p>
  69. * If the Excel format pattern cannot be parsed successfully, then a default
  70. * format will be used. The default number format will mimic the Excel General
  71. * format: "#" for whole numbers and "#.##########" for decimal numbers. You
  72. * can override the default format pattern with <code>
  73. * DataFormatter.setDefaultNumberFormat(Format)</code>. <b>Note:</b> the
  74. * default format will only be used when a Format cannot be created from the
  75. * cell's data format string.
  76. *
  77. * <p>
  78. * Note that by default formatted numeric values are trimmed.
  79. * Excel formats can contain spacers and padding and the default behavior is to strip them off.
  80. * </p>
  81. * <p>Example:</p>
  82. * <p>
  83. * Consider a numeric cell with a value <code>12.343</code> and format <code>"##.##_ "</code>.
  84. * The trailing underscore and space ("_ ") in the format adds a space to the end and Excel formats this cell as <code>"12.34 "</code>,
  85. * but <code>DataFormatter</code> trims the formatted value and returns <code>"12.34"</code>.
  86. * </p>
  87. * You can enable spaces by passing the <code>emulateCsv=true</code> flag in the <code>DateFormatter</code> cosntructor.
  88. * If set to true, then the output tries to conform to what you get when you take an xls or xlsx in Excel and Save As CSV file:
  89. * <ul>
  90. * <li>returned values are not trimmed</li>
  91. * <li>Invalid dates are formatted as 255 pound signs ("#")</li>
  92. * <li>simulate Excel's handling of a format string of all # when the value is 0.
  93. * Excel will output "", <code>DataFormatter</code> will output "0".
  94. * </ul>
  95. */
  96. public class DataFormatter {
  97. private static final String defaultFractionWholePartFormat = "#";
  98. private static final String defaultFractionFractionPartFormat = "#/##";
  99. /** Pattern to find a number format: "0" or "#" */
  100. private static final Pattern numPattern = Pattern.compile("[0#]+");
  101. /** Pattern to find days of week as text "ddd...." */
  102. private static final Pattern daysAsText = Pattern.compile("([d]{3,})", Pattern.CASE_INSENSITIVE);
  103. /** Pattern to find "AM/PM" marker */
  104. private static final Pattern amPmPattern = Pattern.compile("((A|P)[M/P]*)", Pattern.CASE_INSENSITIVE);
  105. /**
  106. * A regex to find locale patterns like [$$-1009] and [$?-452].
  107. * Note that we don't currently process these into locales
  108. */
  109. private static final Pattern localePatternGroup = Pattern.compile("(\\[\\$[^-\\]]*-[0-9A-Z]+\\])");
  110. /**
  111. * A regex to match the colour formattings rules.
  112. * Allowed colours are: Black, Blue, Cyan, Green,
  113. * Magenta, Red, White, Yellow, "Color n" (1<=n<=56)
  114. */
  115. private static final Pattern colorPattern =
  116. Pattern.compile("(\\[BLACK\\])|(\\[BLUE\\])|(\\[CYAN\\])|(\\[GREEN\\])|" +
  117. "(\\[MAGENTA\\])|(\\[RED\\])|(\\[WHITE\\])|(\\[YELLOW\\])|" +
  118. "(\\[COLOR\\s*\\d\\])|(\\[COLOR\\s*[0-5]\\d\\])", Pattern.CASE_INSENSITIVE);
  119. /**
  120. * A regex to identify a fraction pattern.
  121. * This requires that replaceAll("\\?", "#") has already been called
  122. */
  123. private static final Pattern fractionPattern = Pattern.compile("(?:([#\\d]+)\\s+)?(#+)\\s*\\/\\s*([#\\d]+)");
  124. /**
  125. * A regex to strip junk out of fraction formats
  126. */
  127. private static final Pattern fractionStripper = Pattern.compile("(\"[^\"]*\")|([^ \\?#\\d\\/]+)");
  128. /**
  129. * Cells formatted with a date or time format and which contain invalid date or time values
  130. * show 255 pound signs ("#").
  131. */
  132. private static final String invalidDateTimeString;
  133. static {
  134. StringBuilder buf = new StringBuilder();
  135. for(int i = 0; i < 255; i++) buf.append('#');
  136. invalidDateTimeString = buf.toString();
  137. }
  138. /**
  139. * The decimal symbols of the locale used for formatting values.
  140. */
  141. private final DecimalFormatSymbols decimalSymbols;
  142. /**
  143. * The date symbols of the locale used for formatting values.
  144. */
  145. private final DateFormatSymbols dateSymbols;
  146. /** <em>General</em> format for whole numbers. */
  147. private final Format generalWholeNumFormat;
  148. /** <em>General</em> format for decimal numbers. */
  149. private final Format generalDecimalNumFormat;
  150. /** A default format to use when a number pattern cannot be parsed. */
  151. private Format defaultNumFormat;
  152. /**
  153. * A map to cache formats.
  154. * Map<String,Format> formats
  155. */
  156. private final Map<String,Format> formats;
  157. private boolean emulateCsv = false;
  158. /**
  159. * Creates a formatter using the {@link Locale#getDefault() default locale}.
  160. */
  161. public DataFormatter() {
  162. this(false);
  163. }
  164. /**
  165. * Creates a formatter using the {@link Locale#getDefault() default locale}.
  166. *
  167. * @param emulateCsv whether to emulate CSV output.
  168. */
  169. public DataFormatter(boolean emulateCsv) {
  170. this(Locale.getDefault());
  171. this.emulateCsv = emulateCsv;
  172. }
  173. /**
  174. * Creates a formatter using the given locale.
  175. *
  176. * @param emulateCsv whether to emulate CSV output.
  177. */
  178. public DataFormatter(Locale locale, boolean emulateCsv) {
  179. this(locale);
  180. this.emulateCsv = emulateCsv;
  181. }
  182. /**
  183. * Creates a formatter using the given locale.
  184. */
  185. public DataFormatter(Locale locale) {
  186. dateSymbols = new DateFormatSymbols(locale);
  187. decimalSymbols = new DecimalFormatSymbols(locale);
  188. generalWholeNumFormat = new DecimalFormat("#", decimalSymbols);
  189. generalDecimalNumFormat = new DecimalFormat("#.##########", decimalSymbols);
  190. formats = new HashMap<String,Format>();
  191. // init built-in formats
  192. Format zipFormat = ZipPlusFourFormat.instance;
  193. addFormat("00000\\-0000", zipFormat);
  194. addFormat("00000-0000", zipFormat);
  195. Format phoneFormat = PhoneFormat.instance;
  196. // allow for format string variations
  197. addFormat("[<=9999999]###\\-####;\\(###\\)\\ ###\\-####", phoneFormat);
  198. addFormat("[<=9999999]###-####;(###) ###-####", phoneFormat);
  199. addFormat("###\\-####;\\(###\\)\\ ###\\-####", phoneFormat);
  200. addFormat("###-####;(###) ###-####", phoneFormat);
  201. Format ssnFormat = SSNFormat.instance;
  202. addFormat("000\\-00\\-0000", ssnFormat);
  203. addFormat("000-00-0000", ssnFormat);
  204. }
  205. /**
  206. * Return a Format for the given cell if one exists, otherwise try to
  207. * create one. This method will return <code>null</code> if the any of the
  208. * following is true:
  209. * <ul>
  210. * <li>the cell's style is null</li>
  211. * <li>the style's data format string is null or empty</li>
  212. * <li>the format string cannot be recognized as either a number or date</li>
  213. * </ul>
  214. *
  215. * @param cell The cell to retrieve a Format for
  216. * @return A Format for the format String
  217. */
  218. private Format getFormat(Cell cell) {
  219. if ( cell.getCellStyle() == null) {
  220. return null;
  221. }
  222. int formatIndex = cell.getCellStyle().getDataFormat();
  223. String formatStr = cell.getCellStyle().getDataFormatString();
  224. if(formatStr == null || formatStr.trim().length() == 0) {
  225. return null;
  226. }
  227. return getFormat(cell.getNumericCellValue(), formatIndex, formatStr);
  228. }
  229. private Format getFormat(double cellValue, int formatIndex, String formatStrIn) {
  230. // // Might be better to separate out the n p and z formats, falling back to p when n and z are not set.
  231. // // That however would require other code to be re factored.
  232. // String[] formatBits = formatStrIn.split(";");
  233. // int i = cellValue > 0.0 ? 0 : cellValue < 0.0 ? 1 : 2;
  234. // String formatStr = (i < formatBits.length) ? formatBits[i] : formatBits[0];
  235. String formatStr = formatStrIn;
  236. // Excel supports positive/negative/zero, but java
  237. // doesn't, so we need to do it specially
  238. final int firstAt = formatStr.indexOf(';');
  239. final int lastAt = formatStr.lastIndexOf(';');
  240. // p and p;n are ok by default. p;n;z and p;n;z;s need to be fixed.
  241. if (firstAt != -1 && firstAt != lastAt) {
  242. final int secondAt = formatStr.indexOf(';', firstAt + 1);
  243. if (secondAt == lastAt) { // p;n;z
  244. if (cellValue == 0.0) {
  245. formatStr = formatStr.substring(lastAt + 1);
  246. } else {
  247. formatStr = formatStr.substring(0, lastAt);
  248. }
  249. } else {
  250. if (cellValue == 0.0) { // p;n;z;s
  251. formatStr = formatStr.substring(secondAt + 1, lastAt);
  252. } else {
  253. formatStr = formatStr.substring(0, secondAt);
  254. }
  255. }
  256. }
  257. // Excel's # with value 0 will output empty where Java will output 0. This hack removes the # from the format.
  258. if (emulateCsv && cellValue == 0.0 && formatStr.contains("#") && !formatStr.contains("0")) {
  259. formatStr = formatStr.replaceAll("#", "");
  260. }
  261. // See if we already have it cached
  262. Format format = formats.get(formatStr);
  263. if (format != null) {
  264. return format;
  265. }
  266. // Is it one of the special built in types, General or @?
  267. if ("General".equalsIgnoreCase(formatStr) || "@".equals(formatStr)) {
  268. if (isWholeNumber(cellValue)) {
  269. return generalWholeNumFormat;
  270. }
  271. return generalDecimalNumFormat;
  272. }
  273. // Build a formatter, and cache it
  274. format = createFormat(cellValue, formatIndex, formatStr);
  275. formats.put(formatStr, format);
  276. return format;
  277. }
  278. /**
  279. * Create and return a Format based on the format string from a cell's
  280. * style. If the pattern cannot be parsed, return a default pattern.
  281. *
  282. * @param cell The Excel cell
  283. * @return A Format representing the excel format. May return null.
  284. */
  285. public Format createFormat(Cell cell) {
  286. int formatIndex = cell.getCellStyle().getDataFormat();
  287. String formatStr = cell.getCellStyle().getDataFormatString();
  288. return createFormat(cell.getNumericCellValue(), formatIndex, formatStr);
  289. }
  290. private Format createFormat(double cellValue, int formatIndex, String sFormat) {
  291. String formatStr = sFormat;
  292. // Remove colour formatting if present
  293. Matcher colourM = colorPattern.matcher(formatStr);
  294. while(colourM.find()) {
  295. String colour = colourM.group();
  296. // Paranoid replacement...
  297. int at = formatStr.indexOf(colour);
  298. if(at == -1) break;
  299. String nFormatStr = formatStr.substring(0,at) +
  300. formatStr.substring(at+colour.length());
  301. if(nFormatStr.equals(formatStr)) break;
  302. // Try again in case there's multiple
  303. formatStr = nFormatStr;
  304. colourM = colorPattern.matcher(formatStr);
  305. }
  306. // Strip off the locale information, we use an instance-wide locale for everything
  307. Matcher m = localePatternGroup.matcher(formatStr);
  308. while(m.find()) {
  309. String match = m.group();
  310. String symbol = match.substring(match.indexOf('$') + 1, match.indexOf('-'));
  311. if (symbol.indexOf('$') > -1) {
  312. StringBuffer sb = new StringBuffer();
  313. sb.append(symbol.substring(0, symbol.indexOf('$')));
  314. sb.append('\\');
  315. sb.append(symbol.substring(symbol.indexOf('$'), symbol.length()));
  316. symbol = sb.toString();
  317. }
  318. formatStr = m.replaceAll(symbol);
  319. m = localePatternGroup.matcher(formatStr);
  320. }
  321. // Check for special cases
  322. if(formatStr == null || formatStr.trim().length() == 0) {
  323. return getDefaultFormat(cellValue);
  324. }
  325. if ("General".equalsIgnoreCase(formatStr) || "@".equals(formatStr)) {
  326. if (isWholeNumber(cellValue)) {
  327. return generalWholeNumFormat;
  328. }
  329. return generalDecimalNumFormat;
  330. }
  331. if(DateUtil.isADateFormat(formatIndex,formatStr) &&
  332. DateUtil.isValidExcelDate(cellValue)) {
  333. return createDateFormat(formatStr, cellValue);
  334. }
  335. // Excel supports fractions in format strings, which Java doesn't
  336. if (formatStr.indexOf("#/") >= 0 || formatStr.indexOf("?/") >= 0) {
  337. String[] chunks = formatStr.split(";");
  338. for (int i = 0; i < chunks.length; i++){
  339. String chunk = chunks[i].replaceAll("\\?", "#");
  340. Matcher matcher = fractionStripper.matcher(chunk);
  341. chunk = matcher.replaceAll(" ");
  342. chunk = chunk.replaceAll(" +", " ");
  343. Matcher fractionMatcher = fractionPattern.matcher(chunk);
  344. //take the first match
  345. if (fractionMatcher.find()){
  346. String wholePart = (fractionMatcher.group(1) == null) ? "" : defaultFractionWholePartFormat;
  347. return new FractionFormat(wholePart, fractionMatcher.group(3));
  348. }
  349. }
  350. // Strip custom text in quotes and escaped characters for now as it can cause performance problems in fractions.
  351. //String strippedFormatStr = formatStr.replaceAll("\\\\ ", " ").replaceAll("\\\\.", "").replaceAll("\"[^\"]*\"", " ").replaceAll("\\?", "#");
  352. //System.out.println("formatStr: "+strippedFormatStr);
  353. return new FractionFormat(defaultFractionWholePartFormat, defaultFractionFractionPartFormat);
  354. }
  355. if (numPattern.matcher(formatStr).find()) {
  356. return createNumberFormat(formatStr, cellValue);
  357. }
  358. if (emulateCsv) {
  359. return new ConstantStringFormat(cleanFormatForNumber(formatStr));
  360. }
  361. // TODO - when does this occur?
  362. return null;
  363. }
  364. private Format createDateFormat(String pFormatStr, double cellValue) {
  365. String formatStr = pFormatStr;
  366. formatStr = formatStr.replaceAll("\\\\-","-");
  367. formatStr = formatStr.replaceAll("\\\\,",",");
  368. formatStr = formatStr.replaceAll("\\\\\\.","."); // . is a special regexp char
  369. formatStr = formatStr.replaceAll("\\\\ "," ");
  370. formatStr = formatStr.replaceAll("\\\\/","/"); // weird: m\\/d\\/yyyy
  371. formatStr = formatStr.replaceAll(";@", "");
  372. formatStr = formatStr.replaceAll("\"/\"", "/"); // "/" is escaped for no reason in: mm"/"dd"/"yyyy
  373. boolean hasAmPm = false;
  374. Matcher amPmMatcher = amPmPattern.matcher(formatStr);
  375. while (amPmMatcher.find()) {
  376. formatStr = amPmMatcher.replaceAll("@");
  377. hasAmPm = true;
  378. amPmMatcher = amPmPattern.matcher(formatStr);
  379. }
  380. formatStr = formatStr.replaceAll("@", "a");
  381. Matcher dateMatcher = daysAsText.matcher(formatStr);
  382. if (dateMatcher.find()) {
  383. String match = dateMatcher.group(0);
  384. formatStr = dateMatcher.replaceAll(match.toUpperCase().replaceAll("D", "E"));
  385. }
  386. // Convert excel date format to SimpleDateFormat.
  387. // Excel uses lower and upper case 'm' for both minutes and months.
  388. // From Excel help:
  389. /*
  390. The "m" or "mm" code must appear immediately after the "h" or"hh"
  391. code or immediately before the "ss" code; otherwise, Microsoft
  392. Excel displays the month instead of minutes."
  393. */
  394. StringBuffer sb = new StringBuffer();
  395. char[] chars = formatStr.toCharArray();
  396. boolean mIsMonth = true;
  397. List<Integer> ms = new ArrayList<Integer>();
  398. boolean isElapsed = false;
  399. for(int j=0; j<chars.length; j++) {
  400. char c = chars[j];
  401. if (c == '[' && !isElapsed) {
  402. isElapsed = true;
  403. mIsMonth = false;
  404. sb.append(c);
  405. }
  406. else if (c == ']' && isElapsed) {
  407. isElapsed = false;
  408. sb.append(c);
  409. }
  410. else if (isElapsed) {
  411. if (c == 'h' || c == 'H') {
  412. sb.append('H');
  413. }
  414. else if (c == 'm' || c == 'M') {
  415. sb.append('m');
  416. }
  417. else if (c == 's' || c == 'S') {
  418. sb.append('s');
  419. }
  420. else {
  421. sb.append(c);
  422. }
  423. }
  424. else if (c == 'h' || c == 'H') {
  425. mIsMonth = false;
  426. if (hasAmPm) {
  427. sb.append('h');
  428. } else {
  429. sb.append('H');
  430. }
  431. }
  432. else if (c == 'm' || c == 'M') {
  433. if(mIsMonth) {
  434. sb.append('M');
  435. ms.add(
  436. Integer.valueOf(sb.length() -1)
  437. );
  438. } else {
  439. sb.append('m');
  440. }
  441. }
  442. else if (c == 's' || c == 'S') {
  443. sb.append('s');
  444. // if 'M' precedes 's' it should be minutes ('m')
  445. for (int i = 0; i < ms.size(); i++) {
  446. int index = ms.get(i).intValue();
  447. if (sb.charAt(index) == 'M') {
  448. sb.replace(index, index+1, "m");
  449. }
  450. }
  451. mIsMonth = true;
  452. ms.clear();
  453. }
  454. else if (Character.isLetter(c)) {
  455. mIsMonth = true;
  456. ms.clear();
  457. if (c == 'y' || c == 'Y') {
  458. sb.append('y');
  459. }
  460. else if (c == 'd' || c == 'D') {
  461. sb.append('d');
  462. }
  463. else {
  464. sb.append(c);
  465. }
  466. }
  467. else {
  468. sb.append(c);
  469. }
  470. }
  471. formatStr = sb.toString();
  472. try {
  473. return new ExcelStyleDateFormatter(formatStr, dateSymbols);
  474. } catch(IllegalArgumentException iae) {
  475. // the pattern could not be parsed correctly,
  476. // so fall back to the default number format
  477. return getDefaultFormat(cellValue);
  478. }
  479. }
  480. private String cleanFormatForNumber(String formatStr) {
  481. StringBuffer sb = new StringBuffer(formatStr);
  482. if (emulateCsv) {
  483. // Requested spacers with "_" are replaced by a single space.
  484. // Full-column-width padding "*" are removed.
  485. // Not processing fractions at this time. Replace ? with space.
  486. // This matches CSV output.
  487. for (int i = 0; i < sb.length(); i++) {
  488. char c = sb.charAt(i);
  489. if (c == '_' || c == '*' || c == '?') {
  490. if (i > 0 && sb.charAt((i - 1)) == '\\') {
  491. // It's escaped, don't worry
  492. continue;
  493. }
  494. if (c == '?') {
  495. sb.setCharAt(i, ' ');
  496. } else if (i < sb.length() - 1) {
  497. // Remove the character we're supposed
  498. // to match the space of / pad to the
  499. // column width with
  500. if (c == '_') {
  501. sb.setCharAt(i + 1, ' ');
  502. } else {
  503. sb.deleteCharAt(i + 1);
  504. }
  505. // Remove the character too
  506. sb.deleteCharAt(i);
  507. }
  508. }
  509. }
  510. } else {
  511. // If they requested spacers, with "_",
  512. // remove those as we don't do spacing
  513. // If they requested full-column-width
  514. // padding, with "*", remove those too
  515. for (int i = 0; i < sb.length(); i++) {
  516. char c = sb.charAt(i);
  517. if (c == '_' || c == '*') {
  518. if (i > 0 && sb.charAt((i - 1)) == '\\') {
  519. // It's escaped, don't worry
  520. continue;
  521. }
  522. if (i < sb.length() - 1) {
  523. // Remove the character we're supposed
  524. // to match the space of / pad to the
  525. // column width with
  526. sb.deleteCharAt(i + 1);
  527. }
  528. // Remove the _ too
  529. sb.deleteCharAt(i);
  530. }
  531. }
  532. }
  533. // Now, handle the other aspects like
  534. // quoting and scientific notation
  535. for(int i = 0; i < sb.length(); i++) {
  536. char c = sb.charAt(i);
  537. // remove quotes and back slashes
  538. if (c == '\\' || c == '"') {
  539. sb.deleteCharAt(i);
  540. i--;
  541. // for scientific/engineering notation
  542. } else if (c == '+' && i > 0 && sb.charAt(i - 1) == 'E') {
  543. sb.deleteCharAt(i);
  544. i--;
  545. }
  546. }
  547. return sb.toString();
  548. }
  549. private Format createNumberFormat(String formatStr, double cellValue) {
  550. final String format = cleanFormatForNumber(formatStr);
  551. try {
  552. DecimalFormat df = new DecimalFormat(format, decimalSymbols);
  553. setExcelStyleRoundingMode(df);
  554. return df;
  555. } catch(IllegalArgumentException iae) {
  556. // the pattern could not be parsed correctly,
  557. // so fall back to the default number format
  558. return getDefaultFormat(cellValue);
  559. }
  560. }
  561. /**
  562. * Return true if the double value represents a whole number
  563. * @param d the double value to check
  564. * @return <code>true</code> if d is a whole number
  565. */
  566. private static boolean isWholeNumber(double d) {
  567. return d == Math.floor(d);
  568. }
  569. /**
  570. * Returns a default format for a cell.
  571. * @param cell The cell
  572. * @return a default format
  573. */
  574. public Format getDefaultFormat(Cell cell) {
  575. return getDefaultFormat(cell.getNumericCellValue());
  576. }
  577. private Format getDefaultFormat(double cellValue) {
  578. // for numeric cells try user supplied default
  579. if (defaultNumFormat != null) {
  580. return defaultNumFormat;
  581. // otherwise use general format
  582. }
  583. if (isWholeNumber(cellValue)){
  584. return generalWholeNumFormat;
  585. }
  586. return generalDecimalNumFormat;
  587. }
  588. /**
  589. * Performs Excel-style date formatting, using the
  590. * supplied Date and format
  591. */
  592. private String performDateFormatting(Date d, Format dateFormat) {
  593. if(dateFormat != null) {
  594. return dateFormat.format(d);
  595. }
  596. return d.toString();
  597. }
  598. /**
  599. * Returns the formatted value of an Excel date as a <tt>String</tt> based
  600. * on the cell's <code>DataFormat</code>. i.e. "Thursday, January 02, 2003"
  601. * , "01/02/2003" , "02-Jan" , etc.
  602. *
  603. * @param cell The cell
  604. * @return a formatted date string
  605. */
  606. private String getFormattedDateString(Cell cell) {
  607. Format dateFormat = getFormat(cell);
  608. if(dateFormat instanceof ExcelStyleDateFormatter) {
  609. // Hint about the raw excel value
  610. ((ExcelStyleDateFormatter)dateFormat).setDateToBeFormatted(
  611. cell.getNumericCellValue()
  612. );
  613. }
  614. Date d = cell.getDateCellValue();
  615. return performDateFormatting(d, dateFormat);
  616. }
  617. /**
  618. * Returns the formatted value of an Excel number as a <tt>String</tt>
  619. * based on the cell's <code>DataFormat</code>. Supported formats include
  620. * currency, percents, decimals, phone number, SSN, etc.:
  621. * "61.54%", "$100.00", "(800) 555-1234".
  622. *
  623. * @param cell The cell
  624. * @return a formatted number string
  625. */
  626. private String getFormattedNumberString(Cell cell) {
  627. Format numberFormat = getFormat(cell);
  628. double d = cell.getNumericCellValue();
  629. if (numberFormat == null) {
  630. return String.valueOf(d);
  631. }
  632. return numberFormat.format(new Double(d));
  633. }
  634. /**
  635. * Formats the given raw cell value, based on the supplied
  636. * format index and string, according to excel style rules.
  637. * @see #formatCellValue(Cell)
  638. */
  639. public String formatRawCellContents(double value, int formatIndex, String formatString) {
  640. return formatRawCellContents(value, formatIndex, formatString, false);
  641. }
  642. /**
  643. * Formats the given raw cell value, based on the supplied
  644. * format index and string, according to excel style rules.
  645. * @see #formatCellValue(Cell)
  646. */
  647. public String formatRawCellContents(double value, int formatIndex, String formatString, boolean use1904Windowing) {
  648. // Is it a date?
  649. if(DateUtil.isADateFormat(formatIndex,formatString)) {
  650. if(DateUtil.isValidExcelDate(value)) {
  651. Format dateFormat = getFormat(value, formatIndex, formatString);
  652. if(dateFormat instanceof ExcelStyleDateFormatter) {
  653. // Hint about the raw excel value
  654. ((ExcelStyleDateFormatter)dateFormat).setDateToBeFormatted(value);
  655. }
  656. Date d = DateUtil.getJavaDate(value, use1904Windowing);
  657. return performDateFormatting(d, dateFormat);
  658. }
  659. // RK: Invalid dates are 255 #s.
  660. if (emulateCsv) {
  661. return invalidDateTimeString;
  662. }
  663. }
  664. // else Number
  665. Format numberFormat = getFormat(value, formatIndex, formatString);
  666. if (numberFormat == null) {
  667. return String.valueOf(value);
  668. }
  669. // RK: This hack handles scientific notation by adding the missing + back.
  670. String result = numberFormat.format(new Double(value));
  671. if (result.contains("E") && !result.contains("E-")) {
  672. result = result.replaceFirst("E", "E+");
  673. }
  674. return result;
  675. }
  676. /**
  677. * <p>
  678. * Returns the formatted value of a cell as a <tt>String</tt> regardless
  679. * of the cell type. If the Excel format pattern cannot be parsed then the
  680. * cell value will be formatted using a default format.
  681. * </p>
  682. * <p>When passed a null or blank cell, this method will return an empty
  683. * String (""). Formulas in formula type cells will not be evaluated.
  684. * </p>
  685. *
  686. * @param cell The cell
  687. * @return the formatted cell value as a String
  688. */
  689. public String formatCellValue(Cell cell) {
  690. return formatCellValue(cell, null);
  691. }
  692. /**
  693. * <p>
  694. * Returns the formatted value of a cell as a <tt>String</tt> regardless
  695. * of the cell type. If the Excel format pattern cannot be parsed then the
  696. * cell value will be formatted using a default format.
  697. * </p>
  698. * <p>When passed a null or blank cell, this method will return an empty
  699. * String (""). Formula cells will be evaluated using the given
  700. * {@link FormulaEvaluator} if the evaluator is non-null. If the
  701. * evaluator is null, then the formula String will be returned. The caller
  702. * is responsible for setting the currentRow on the evaluator
  703. *</p>
  704. *
  705. * @param cell The cell (can be null)
  706. * @param evaluator The FormulaEvaluator (can be null)
  707. * @return a string value of the cell
  708. */
  709. public String formatCellValue(Cell cell, FormulaEvaluator evaluator) {
  710. if (cell == null) {
  711. return "";
  712. }
  713. int cellType = cell.getCellType();
  714. if (cellType == Cell.CELL_TYPE_FORMULA) {
  715. if (evaluator == null) {
  716. return cell.getCellFormula();
  717. }
  718. cellType = evaluator.evaluateFormulaCell(cell);
  719. }
  720. switch (cellType) {
  721. case Cell.CELL_TYPE_NUMERIC :
  722. if (DateUtil.isCellDateFormatted(cell)) {
  723. return getFormattedDateString(cell);
  724. }
  725. return getFormattedNumberString(cell);
  726. case Cell.CELL_TYPE_STRING :
  727. return cell.getRichStringCellValue().getString();
  728. case Cell.CELL_TYPE_BOOLEAN :
  729. return String.valueOf(cell.getBooleanCellValue());
  730. case Cell.CELL_TYPE_BLANK :
  731. return "";
  732. }
  733. throw new RuntimeException("Unexpected celltype (" + cellType + ")");
  734. }
  735. /**
  736. * <p>
  737. * Sets a default number format to be used when the Excel format cannot be
  738. * parsed successfully. <b>Note:</b> This is a fall back for when an error
  739. * occurs while parsing an Excel number format pattern. This will not
  740. * affect cells with the <em>General</em> format.
  741. * </p>
  742. * <p>
  743. * The value that will be passed to the Format's format method (specified
  744. * by <code>java.text.Format#format</code>) will be a double value from a
  745. * numeric cell. Therefore the code in the format method should expect a
  746. * <code>Number</code> value.
  747. * </p>
  748. *
  749. * @param format A Format instance to be used as a default
  750. * @see java.text.Format#format
  751. */
  752. public void setDefaultNumberFormat(Format format) {
  753. Iterator<Map.Entry<String,Format>> itr = formats.entrySet().iterator();
  754. while(itr.hasNext()) {
  755. Map.Entry<String,Format> entry = itr.next();
  756. if (entry.getValue() == generalDecimalNumFormat
  757. || entry.getValue() == generalWholeNumFormat) {
  758. entry.setValue(format);
  759. }
  760. }
  761. defaultNumFormat = format;
  762. }
  763. /**
  764. * Adds a new format to the available formats.
  765. * <p>
  766. * The value that will be passed to the Format's format method (specified
  767. * by <code>java.text.Format#format</code>) will be a double value from a
  768. * numeric cell. Therefore the code in the format method should expect a
  769. * <code>Number</code> value.
  770. * </p>
  771. * @param excelFormatStr The data format string
  772. * @param format A Format instance
  773. */
  774. public void addFormat(String excelFormatStr, Format format) {
  775. formats.put(excelFormatStr, format);
  776. }
  777. // Some custom formats
  778. /**
  779. * @return a <tt>DecimalFormat</tt> with parseIntegerOnly set <code>true</code>
  780. */
  781. /* package */ static DecimalFormat createIntegerOnlyFormat(String fmt) {
  782. DecimalFormat result = new DecimalFormat(fmt);
  783. result.setParseIntegerOnly(true);
  784. return result;
  785. }
  786. /**
  787. * Enables excel style rounding mode (round half up)
  788. * on the Decimal Format if possible.
  789. * This will work for Java 1.6, but isn't possible
  790. * on Java 1.5.
  791. */
  792. public static void setExcelStyleRoundingMode(DecimalFormat format) {
  793. setExcelStyleRoundingMode(format, RoundingMode.HALF_UP);
  794. }
  795. /**
  796. * Enables custom rounding mode
  797. * on the Decimal Format if possible.
  798. * This will work for Java 1.6, but isn't possible
  799. * on Java 1.5.
  800. * @param format DecimalFormat
  801. * @param roundingMode RoundingMode
  802. */
  803. public static void setExcelStyleRoundingMode(DecimalFormat format, RoundingMode roundingMode) {
  804. try {
  805. Method srm = format.getClass().getMethod("setRoundingMode", RoundingMode.class);
  806. srm.invoke(format, roundingMode);
  807. } catch(NoSuchMethodException e) {
  808. // Java 1.5
  809. } catch(IllegalAccessException iae) {
  810. // Shouldn't happen
  811. throw new RuntimeException("Unable to set rounding mode", iae);
  812. } catch(InvocationTargetException ite) {
  813. // Shouldn't happen
  814. throw new RuntimeException("Unable to set rounding mode", ite);
  815. } catch(SecurityException se) {
  816. // Not much we can do here
  817. }
  818. }
  819. /**
  820. * Format class for Excel's SSN format. This class mimics Excel's built-in
  821. * SSN formatting.
  822. *
  823. * @author James May
  824. */
  825. @SuppressWarnings("serial")
  826. private static final class SSNFormat extends Format {
  827. public static final Format instance = new SSNFormat();
  828. private static final DecimalFormat df = createIntegerOnlyFormat("000000000");
  829. private SSNFormat() {
  830. // enforce singleton
  831. }
  832. /** Format a number as an SSN */
  833. public static String format(Number num) {
  834. String result = df.format(num);
  835. StringBuffer sb = new StringBuffer();
  836. sb.append(result.substring(0, 3)).append('-');
  837. sb.append(result.substring(3, 5)).append('-');
  838. sb.append(result.substring(5, 9));
  839. return sb.toString();
  840. }
  841. public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
  842. return toAppendTo.append(format((Number)obj));
  843. }
  844. public Object parseObject(String source, ParsePosition pos) {
  845. return df.parseObject(source, pos);
  846. }
  847. }
  848. /**
  849. * Format class for Excel Zip + 4 format. This class mimics Excel's
  850. * built-in formatting for Zip + 4.
  851. * @author James May
  852. */
  853. @SuppressWarnings("serial")
  854. private static final class ZipPlusFourFormat extends Format {
  855. public static final Format instance = new ZipPlusFourFormat();
  856. private static final DecimalFormat df = createIntegerOnlyFormat("000000000");
  857. private ZipPlusFourFormat() {
  858. // enforce singleton
  859. }
  860. /** Format a number as Zip + 4 */
  861. public static String format(Number num) {
  862. String result = df.format(num);
  863. StringBuffer sb = new StringBuffer();
  864. sb.append(result.substring(0, 5)).append('-');
  865. sb.append(result.substring(5, 9));
  866. return sb.toString();
  867. }
  868. public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
  869. return toAppendTo.append(format((Number)obj));
  870. }
  871. public Object parseObject(String source, ParsePosition pos) {
  872. return df.parseObject(source, pos);
  873. }
  874. }
  875. /**
  876. * Format class for Excel phone number format. This class mimics Excel's
  877. * built-in phone number formatting.
  878. * @author James May
  879. */
  880. @SuppressWarnings("serial")
  881. private static final class PhoneFormat extends Format {
  882. public static final Format instance = new PhoneFormat();
  883. private static final DecimalFormat df = createIntegerOnlyFormat("##########");
  884. private PhoneFormat() {
  885. // enforce singleton
  886. }
  887. /** Format a number as a phone number */
  888. public static String format(Number num) {
  889. String result = df.format(num);
  890. StringBuffer sb = new StringBuffer();
  891. String seg1, seg2, seg3;
  892. int len = result.length();
  893. if (len <= 4) {
  894. return result;
  895. }
  896. seg3 = result.substring(len - 4, len);
  897. seg2 = result.substring(Math.max(0, len - 7), len - 4);
  898. seg1 = result.substring(Math.max(0, len - 10), Math.max(0, len - 7));
  899. if(seg1 != null && seg1.trim().length() > 0) {
  900. sb.append('(').append(seg1).append(") ");
  901. }
  902. if(seg2 != null && seg2.trim().length() > 0) {
  903. sb.append(seg2).append('-');
  904. }
  905. sb.append(seg3);
  906. return sb.toString();
  907. }
  908. public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
  909. return toAppendTo.append(format((Number)obj));
  910. }
  911. public Object parseObject(String source, ParsePosition pos) {
  912. return df.parseObject(source, pos);
  913. }
  914. }
  915. /**
  916. * Format class that does nothing and always returns a constant string.
  917. *
  918. * This format is used to simulate Excel's handling of a format string
  919. * of all # when the value is 0. Excel will output "", Java will output "0".
  920. *
  921. * @see DataFormatter#createFormat(double, int, String)
  922. */
  923. @SuppressWarnings("serial")
  924. private static final class ConstantStringFormat extends Format {
  925. private static final DecimalFormat df = createIntegerOnlyFormat("##########");
  926. private final String str;
  927. public ConstantStringFormat(String s) {
  928. str = s;
  929. }
  930. @Override
  931. public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
  932. return toAppendTo.append(str);
  933. }
  934. @Override
  935. public Object parseObject(String source, ParsePosition pos) {
  936. return df.parseObject(source, pos);
  937. }
  938. }
  939. }