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