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.

XSSFDataFormat.java 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.xssf.usermodel;
  16. import org.apache.poi.ss.usermodel.BuiltinFormats;
  17. import org.apache.poi.ss.usermodel.DataFormat;
  18. import org.apache.poi.xssf.model.StylesTable;
  19. /**
  20. * Handles data formats for XSSF.
  21. *
  22. * Per Microsoft Excel 2007+ format limitations:
  23. * Workbooks support between 200 and 250 "number formats"
  24. * (POI calls them "data formats") So short or even byte
  25. * would be acceptable data types to use for referring to
  26. * data format indices.
  27. * https://support.office.com/en-us/article/excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3
  28. *
  29. */
  30. public class XSSFDataFormat implements DataFormat {
  31. private final StylesTable stylesSource;
  32. protected XSSFDataFormat(StylesTable stylesSource) {
  33. this.stylesSource = stylesSource;
  34. }
  35. /**
  36. * Get the format index that matches the given format
  37. * string, creating a new format entry if required.
  38. * Aliases text to the proper format as required.
  39. *
  40. * @param format string matching a built-in format
  41. * @return index of format.
  42. */
  43. @Override
  44. public short getFormat(String format) {
  45. int idx = BuiltinFormats.getBuiltinFormat(format);
  46. if(idx == -1) idx = stylesSource.putNumberFormat(format);
  47. return (short)idx;
  48. }
  49. /**
  50. * get the format string that matches the given format index
  51. * @param index of a format
  52. * @return string represented at index of format or <code>null</code> if there is not a format at that index
  53. */
  54. @Override
  55. public String getFormat(short index) {
  56. // Indices used for built-in formats may be overridden with
  57. // custom formats, such as locale-specific currency.
  58. // See org.apache.poi.xssf.usermodel.TestXSSFDataFormat#test49928()
  59. // or bug 49928 for an example.
  60. // This is why we need to check stylesSource first and only fall back to
  61. // BuiltinFormats if the format hasn't been overridden.
  62. String fmt = stylesSource.getNumberFormatAt(index);
  63. if(fmt == null) fmt = BuiltinFormats.getBuiltinFormat(index);
  64. return fmt;
  65. }
  66. /**
  67. * Add a number format with a specific ID into the number format style table.
  68. * If a format with the same ID already exists, overwrite the format code
  69. * with <code>fmt</code>
  70. * This may be used to override built-in number formats.
  71. *
  72. * @param index the number format ID
  73. * @param format the number format code
  74. */
  75. public void putFormat(short index, String format) {
  76. stylesSource.putNumberFormat(index, format);
  77. }
  78. }