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.

HSSFDataFormatter.java 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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.hssf.usermodel;
  16. import java.text.DecimalFormat;
  17. import java.text.Format;
  18. import java.util.Locale;
  19. import org.apache.poi.ss.usermodel.DataFormatter;
  20. import org.apache.poi.util.LocaleUtil;
  21. /**
  22. * HSSFDataFormatter contains methods for formatting the value stored in an
  23. * HSSFCell. This can be useful for reports and GUI presentations when you
  24. * need to display data exactly as it appears in Excel. Supported formats
  25. * include currency, SSN, percentages, decimals, dates, phone numbers, zip
  26. * codes, etc.
  27. * <p>
  28. * Internally, formats will be implemented using subclasses of {@link Format}
  29. * such as {@link DecimalFormat} and {@link java.text.SimpleDateFormat}. Therefore the
  30. * formats used by this class must obey the same pattern rules as these Format
  31. * subclasses. This means that only legal number pattern characters ("0", "#",
  32. * ".", "," etc.) may appear in number formats. Other characters can be
  33. * inserted <em>before</em> or <em> after</em> the number pattern to form a
  34. * prefix or suffix.
  35. * </p>
  36. * <p>
  37. * For example the Excel pattern {@code "$#,##0.00 "USD"_);($#,##0.00 "USD")"
  38. * } will be correctly formatted as "$1,000.00 USD" or "($1,000.00 USD)".
  39. * However the pattern {@code "00-00-00"} is incorrectly formatted by
  40. * DecimalFormat as "000000--". For Excel formats that are not compatible with
  41. * DecimalFormat, you can provide your own custom {@link Format} implementation
  42. * via {@code HSSFDataFormatter.addFormat(String,Format)}. The following
  43. * custom formats are already provided by this class:
  44. * </p>
  45. * <ul><li>SSN "000-00-0000"</li>
  46. * <li>Phone Number "(###) ###-####"</li>
  47. * <li>Zip plus 4 "00000-0000"</li>
  48. * </ul>
  49. * <p>
  50. * If the Excel format pattern cannot be parsed successfully, then a default
  51. * format will be used. The default number format will mimic the Excel General
  52. * format: "#" for whole numbers and "#.##########" for decimal numbers. You
  53. * can override the default format pattern with {@code
  54. * HSSFDataFormatter.setDefaultNumberFormat(Format)}. <b>Note:</b> the
  55. * default format will only be used when a Format cannot be created from the
  56. * cell's data format string.
  57. */
  58. public final class HSSFDataFormatter extends DataFormatter {
  59. /**
  60. * Creates a formatter using the given locale.
  61. */
  62. public HSSFDataFormatter(Locale locale) {
  63. super(locale);
  64. }
  65. /**
  66. * Creates a formatter using the {@link Locale#getDefault() default locale}.
  67. */
  68. public HSSFDataFormatter() {
  69. this(LocaleUtil.getUserLocale());
  70. }
  71. }