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
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 ==================================================================== */
22 * Created on December 18, 2001, 12:42 PM
24 package org
.apache
.poi
.hssf
.usermodel
;
26 import org
.apache
.poi
.hssf
.model
.Workbook
;
27 import org
.apache
.poi
.hssf
.record
.FormatRecord
;
29 import java
.util
.Iterator
;
30 import java
.util
.List
;
31 import java
.util
.ListIterator
;
32 import java
.util
.Vector
;
35 * Utility to identify builtin formats. Now can handle user defined data formats also. The following is a list of the formats as
36 * returned by this class.<P>
43 * 5, "($#,##0_);($#,##0)"<br>
44 * 6, "($#,##0_);[Red]($#,##0)"<br>
45 * 7, "($#,##0.00);($#,##0.00)"<br>
46 * 8, "($#,##0.00_);[Red]($#,##0.00)"<br>
56 * 0x12, "h:mm AM/PM"<br>
57 * 0x13, "h:mm:ss AM/PM"<br>
60 * 0x16, "m/d/yy h:mm"<br>
62 * // 0x17 - 0x24 reserved for international and undocumented
63 * 0x25, "(#,##0_);(#,##0)"<P>
64 * 0x26, "(#,##0_);[Red](#,##0)"<P>
65 * 0x27, "(#,##0.00_);(#,##0.00)"<P>
66 * 0x28, "(#,##0.00_);[Red](#,##0.00)"<P>
67 * 0x29, "_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)"<P>
68 * 0x2a, "_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)"<P>
69 * 0x2b, "_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)"<P>
70 * 0x2c, "_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)"<P>
72 * 0x2e, "[h]:mm:ss"<P>
75 * 0x31, "@" - This is text format.<P>
76 * 0x31 "text" - Alias for "@"<P>
78 * @author Andrew C. Oliver (acoliver at apache dot org)
79 * @author Shawn M. Laubach (slaubach at apache dot org)
82 public class HSSFDataFormat
84 private static List builtinFormats
= createBuiltinFormats();
86 private Vector formats
= new Vector();
87 private Workbook workbook
;
88 private boolean movedBuiltins
= false; // Flag to see if need to
89 // check the built in list
90 // or if the regular list
94 * Construncts a new data formatter. It takes a workbook to have
95 * access to the workbooks format records.
96 * @param workbook the workbook the formats are tied to.
98 public HSSFDataFormat( Workbook workbook
)
100 this.workbook
= workbook
;
101 Iterator i
= workbook
.getFormats().iterator();
102 while ( i
.hasNext() )
104 FormatRecord r
= (FormatRecord
) i
.next();
105 if ( formats
.size() < r
.getIndexCode() + 1 )
107 formats
.setSize( r
.getIndexCode() + 1 );
109 formats
.set( r
.getIndexCode(), r
.getFormatString() );
114 private static synchronized List
createBuiltinFormats()
116 List builtinFormats
= new Vector();
117 builtinFormats
.add( 0, "General" );
118 builtinFormats
.add( 1, "0" );
119 builtinFormats
.add( 2, "0.00" );
120 builtinFormats
.add( 3, "#,##0" );
121 builtinFormats
.add( 4, "#,##0.00" );
122 builtinFormats
.add( 5, "($#,##0_);($#,##0)" );
123 builtinFormats
.add( 6, "($#,##0_);[Red]($#,##0)" );
124 builtinFormats
.add( 7, "($#,##0.00);($#,##0.00)" );
125 builtinFormats
.add( 8, "($#,##0.00_);[Red]($#,##0.00)" );
126 builtinFormats
.add( 9, "0%" );
127 builtinFormats
.add( 0xa, "0.00%" );
128 builtinFormats
.add( 0xb, "0.00E+00" );
129 builtinFormats
.add( 0xc, "# ?/?" );
130 builtinFormats
.add( 0xd, "# ??/??" );
131 builtinFormats
.add( 0xe, "m/d/yy" );
132 builtinFormats
.add( 0xf, "d-mmm-yy" );
133 builtinFormats
.add( 0x10, "d-mmm" );
134 builtinFormats
.add( 0x11, "mmm-yy" );
135 builtinFormats
.add( 0x12, "h:mm AM/PM" );
136 builtinFormats
.add( 0x13, "h:mm:ss AM/PM" );
137 builtinFormats
.add( 0x14, "h:mm" );
138 builtinFormats
.add( 0x15, "h:mm:ss" );
139 builtinFormats
.add( 0x16, "m/d/yy h:mm" );
141 // 0x17 - 0x24 reserved for international and undocumented
142 builtinFormats
.add( 0x17, "0x17" );
143 builtinFormats
.add( 0x18, "0x18" );
144 builtinFormats
.add( 0x19, "0x19" );
145 builtinFormats
.add( 0x1a, "0x1a" );
146 builtinFormats
.add( 0x1b, "0x1b" );
147 builtinFormats
.add( 0x1c, "0x1c" );
148 builtinFormats
.add( 0x1d, "0x1d" );
149 builtinFormats
.add( 0x1e, "0x1e" );
150 builtinFormats
.add( 0x1f, "0x1f" );
151 builtinFormats
.add( 0x20, "0x20" );
152 builtinFormats
.add( 0x21, "0x21" );
153 builtinFormats
.add( 0x22, "0x22" );
154 builtinFormats
.add( 0x23, "0x23" );
155 builtinFormats
.add( 0x24, "0x24" );
157 // 0x17 - 0x24 reserved for international and undocumented
158 builtinFormats
.add( 0x25, "(#,##0_);(#,##0)" );
159 builtinFormats
.add( 0x26, "(#,##0_);[Red](#,##0)" );
160 builtinFormats
.add( 0x27, "(#,##0.00_);(#,##0.00)" );
161 builtinFormats
.add( 0x28, "(#,##0.00_);[Red](#,##0.00)" );
162 builtinFormats
.add( 0x29, "_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)" );
163 builtinFormats
.add( 0x2a, "_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)" );
164 builtinFormats
.add( 0x2b, "_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)" );
165 builtinFormats
.add( 0x2c,
166 "_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)" );
167 builtinFormats
.add( 0x2d, "mm:ss" );
168 builtinFormats
.add( 0x2e, "[h]:mm:ss" );
169 builtinFormats
.add( 0x2f, "mm:ss.0" );
170 builtinFormats
.add( 0x30, "##0.0E+0" );
171 builtinFormats
.add( 0x31, "@" );
172 return builtinFormats
;
175 public static List
getBuiltinFormats()
177 return builtinFormats
;
181 * get the format index that matches the given format string<p>
182 * Automatically converts "text" to excel's format string to represent text.
183 * @param format string matching a built in format
184 * @return index of format or -1 if undefined.
187 public static short getBuiltinFormat( String format
)
189 if (format
.toUpperCase().equals("TEXT"))
194 for (short k
= 0; k
<= 0x31; k
++)
196 String nformat
= (String
) builtinFormats
.get( k
);
198 if ( ( nformat
!= null ) && nformat
.equals( format
) )
208 * Get the format index that matches the given format
209 * string, creating a new format entry if required.
210 * Aliases text to the proper format as required.
211 * @param format string matching a built in format
212 * @return index of format.
214 public short getFormat( String format
)
219 if (format
.toUpperCase().equals("TEXT"))
222 if ( !movedBuiltins
)
224 i
= builtinFormats
.listIterator();
225 while ( i
.hasNext() )
228 if ( formats
.size() < ind
+ 1 )
230 formats
.setSize( ind
+ 1 );
233 formats
.set( ind
, i
.next() );
235 movedBuiltins
= true;
237 i
= formats
.listIterator();
238 while ( i
.hasNext() )
241 if ( format
.equals( i
.next() ) )
245 ind
= workbook
.getFormat( format
, true );
246 if ( formats
.size() <= ind
)
247 formats
.setSize( ind
+ 1 );
248 formats
.set( ind
, format
);
254 * get the format string that matches the given format index
255 * @param index of a format
256 * @return string represented at index of format or null if there is not a format at that index
259 public String
getFormat( short index
)
262 return (String
) formats
.get( index
);
264 return (String
) ( builtinFormats
.size() > index
265 && builtinFormats
.get( index
) != null
266 ? builtinFormats
.get( index
) : formats
.get( index
) );
270 * get the format string that matches the given format index
271 * @param index of a built in format
272 * @return string represented at index of format or null if there is not a builtin format at that index
273 * @throws ArrayOutOfBoundsException when the index exceeds the number of builtin formats.
276 public static String
getBuiltinFormat( short index
)
278 return (String
) builtinFormats
.get( index
);
282 * get the number of builtin and reserved builtinFormats
283 * @return number of builtin and reserved builtinFormats
286 public static int getNumberOfBuiltinBuiltinFormats()
288 return builtinFormats
.size();