1 16 17 18 23 package org.apache.poi.hssf.usermodel; 24 25 import org.apache.poi.hssf.model.Workbook; 26 import org.apache.poi.hssf.record.FormatRecord; 27 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.ListIterator ; 31 import java.util.Vector ; 32 33 80 81 public class HSSFDataFormat 82 { 83 private static Vector builtinFormats; 84 85 private Vector formats = new Vector (); 86 private Workbook workbook; 87 private boolean movedBuiltins = false; 92 97 public HSSFDataFormat( Workbook workbook ) 98 { 99 this.workbook = workbook; 100 if ( builtinFormats == null ) populateBuiltinFormats(); 101 Iterator i = workbook.getFormats().iterator(); 102 while ( i.hasNext() ) 103 { 104 FormatRecord r = (FormatRecord) i.next(); 105 if ( formats.size() < r.getIndexCode() + 1 ) 106 { 107 formats.setSize( r.getIndexCode() + 1 ); 108 } 109 formats.set( r.getIndexCode(), r.getFormatString() ); 110 } 111 112 } 113 114 private static synchronized void populateBuiltinFormats() 115 { 116 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" ); 140 141 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" ); 156 157 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 } 173 174 public static List getBuiltinFormats() 175 { 176 if ( builtinFormats == null ) 177 { 178 populateBuiltinFormats(); 179 } 180 return builtinFormats; 181 } 182 183 189 190 public static short getBuiltinFormat( String format ) 191 { 192 if (format.toUpperCase().equals("TEXT")) 193 format = "@"; 194 195 if ( builtinFormats == null ) 196 { 197 populateBuiltinFormats(); 198 } 199 short retval = -1; 200 201 for (short k = 0; k <= 0x31; k++) 202 { 203 String nformat = (String ) builtinFormats.get( k ); 204 205 if ( ( nformat != null ) && nformat.equals( format ) ) 206 { 207 retval = k; 208 break; 209 } 210 } 211 return retval; 212 } 213 214 220 221 public short getFormat( String format ) 222 { 223 ListIterator i; 224 int ind; 225 226 if (format.toUpperCase().equals("TEXT")) 227 format = "@"; 228 229 if ( !movedBuiltins ) 230 { 231 i = builtinFormats.listIterator(); 232 while ( i.hasNext() ) 233 { 234 ind = i.nextIndex(); 235 formats.add( ind, i.next() ); 236 } 237 movedBuiltins = true; 238 } 239 i = formats.listIterator(); 240 while ( i.hasNext() ) 241 { 242 ind = i.nextIndex(); 243 if ( format.equals( i.next() ) ) 244 return (short) ind; 245 } 246 247 ind = workbook.getFormat( format, true ); 248 if ( formats.size() <= ind ) 249 formats.setSize( ind + 1 ); 250 formats.add( ind, format ); 251 252 return (short) ind; 253 } 254 255 260 261 public String getFormat( short index ) 262 { 263 if ( movedBuiltins ) 264 return (String ) formats.get( index ); 265 else 266 return (String ) ( builtinFormats.size() > index 267 && builtinFormats.get( index ) != null 268 ? builtinFormats.get( index ) : formats.get( index ) ); 269 } 270 271 276 277 public static String getBuiltinFormat( short index ) 278 { 279 if ( builtinFormats == null ) 280 { 281 populateBuiltinFormats(); 282 } 283 return (String ) builtinFormats.get( index ); 284 } 285 286 290 291 public static int getNumberOfBuiltinBuiltinFormats() 292 { 293 if ( builtinFormats == null ) 294 { 295 populateBuiltinFormats(); 296 } 297 return builtinFormats.size(); 298 } 299 } 300 | Popular Tags |