View | Details | Raw Unified | Return to bug 13248
Collapse All | Expand All

(-)src/java/org/apache/poi/hssf/record/FormatRecord.java (-3 / +26 lines)
Lines 64-69 Link Here
64
 *
64
 *
65
 * REFERENCE:  PG 317 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
65
 * REFERENCE:  PG 317 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
66
 * @author Andrew C. Oliver (acoliver at apache dot org)
66
 * @author Andrew C. Oliver (acoliver at apache dot org)
67
 * @author Shawn M. Laubach (shawnlaubach at cox dot net)  
67
 * @version 2.0-pre
68
 * @version 2.0-pre
68
 */
69
 */
69
70
Lines 72-78 Link Here
72
{
73
{
73
    public final static short sid = 0x41e;
74
    public final static short sid = 0x41e;
74
    private short             field_1_index_code;
75
    private short             field_1_index_code;
75
    private byte              field_2_formatstring_len;
76
    private short             field_2_formatstring_len;
76
    private short             field_3_unicode_len;      // unicode string length
77
    private short             field_3_unicode_len;      // unicode string length
77
    private boolean          field_3_unicode_flag;     // it is not undocumented - it is unicode flag
78
    private boolean          field_3_unicode_flag;     // it is not undocumented - it is unicode flag
78
    private String            field_4_formatstring;
79
    private String            field_4_formatstring;
Lines 121-126 Link Here
121
        field_1_index_code       = LittleEndian.getShort(data, 0 + offset);
122
        field_1_index_code       = LittleEndian.getShort(data, 0 + offset);
122
        // field_2_formatstring_len = data[ 2 + offset ];
123
        // field_2_formatstring_len = data[ 2 + offset ];
123
        field_3_unicode_len      = LittleEndian.getShort( data, 2 + offset );
124
        field_3_unicode_len      = LittleEndian.getShort( data, 2 + offset );
125
	field_2_formatstring_len = field_3_unicode_len;
124
        field_3_unicode_flag     = ( data[ 4 + offset ] & (byte)0x01 ) != 0;
126
        field_3_unicode_flag     = ( data[ 4 + offset ] & (byte)0x01 ) != 0;
125
                                              
127
                                              
126
                                              
128
                                              
Lines 156-161 Link Here
156
    public void setFormatStringLength(byte len)
158
    public void setFormatStringLength(byte len)
157
    {
159
    {
158
        field_2_formatstring_len = len;
160
        field_2_formatstring_len = len;
161
	field_3_unicode_len = len;
162
    }
163
164
    /**
165
     * set whether the string is unicode
166
     *
167
     * @param unicode flag for whether string is unicode
168
     */
169
170
    public void setUnicodeFlag(boolean unicode) {
171
	field_3_unicode_flag = unicode;
159
    }
172
    }
160
173
161
    /**
174
    /**
Lines 189-198 Link Here
189
     * @see #getFormatString()
202
     * @see #getFormatString()
190
     */
203
     */
191
204
192
    public byte getFormatStringLength()
205
    public short getFormatStringLength()
193
    {
206
    {
194
        return field_2_formatstring_len;
207
        return field_3_unicode_flag ? field_3_unicode_len : field_2_formatstring_len;
195
    }
208
    }
209
210
    /**
211
     * get whether the string is unicode
212
     *
213
     * @return flag for whether string is unicode
214
     */
215
216
    public boolean getUnicodeFlag() {
217
	return field_3_unicode_flag;
218
    }    
196
219
197
    /**
220
    /**
198
     * get the format string
221
     * get the format string
(-)src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java (-1 / +49 lines)
Lines 253-261 Link Here
253
                     sheet.getRow(( short ) 0).getCell(( short ) 0);
253
                     sheet.getRow(( short ) 0).getCell(( short ) 0);
254
254
255
        assertEquals(1.25,cell.getNumericCellValue(), 1e-10);
255
        assertEquals(1.25,cell.getNumericCellValue(), 1e-10);
256
                         
257
256
258
	assertEquals(format.getFormat(cell.getCellStyle().getDataFormat()), "0.0");
257
	assertEquals(format.getFormat(cell.getCellStyle().getDataFormat()), "0.0");
258
        stream.close();
259
    }
260
261
/**
262
     * TEST NAME:  Test Read/Write Simple w/ Data Format<P>
263
     * OBJECTIVE:  Test that HSSF can write a sheet with custom data formats and then read it and get the proper formats.<P>
264
     * SUCCESS:    HSSF reads the sheet.  Matches values in their particular positions and format is correct<P>
265
     * FAILURE:    HSSF does not read a sheet or excepts.  HSSF cannot identify values
266
     *             in the sheet in their known positions.<P>
267
     *
268
     */
269
270
    public void testWriteDataFormat()
271
        throws IOException
272
    {
273
	File             file = File.createTempFile("testWriteDataFormat",
274
                                                    ".xls");
275
	System.err.println(file);
276
        FileOutputStream out  = new FileOutputStream(file);
277
        HSSFWorkbook     wb   = new HSSFWorkbook();
278
        HSSFSheet        s    = wb.createSheet();
279
        HSSFRow          r    = null;
280
        HSSFCell         c    = null;
281
	HSSFDataFormat format = wb.createDataFormat();
282
	HSSFCellStyle    cs   = wb.createCellStyle();
283
	
284
	short df = format.getFormat("0.0");
285
	cs.setDataFormat(df);
286
	
287
	r = s.createRow((short)0);
288
	c = r.createCell((short)0);
289
	c.setCellStyle(cs);
290
	c.setCellValue(1.25);
291
292
        wb.write(out);
293
        out.close();
294
295
        FileInputStream stream   = new FileInputStream(file);
296
        POIFSFileSystem fs       = new POIFSFileSystem(stream);
297
        HSSFWorkbook    workbook = new HSSFWorkbook(fs);
298
        HSSFSheet       sheet    = workbook.getSheetAt(0);
299
	HSSFCell	cell	 = 
300
                     sheet.getRow(( short ) 0).getCell(( short ) 0);
301
	format = workbook.createDataFormat();
302
303
        assertEquals(1.25,cell.getNumericCellValue(), 1e-10);
304
305
	assertEquals(format.getFormat(df), "0.0");
306
	
259
        stream.close();
307
        stream.close();
260
    }
308
    }
261
309

Return to bug 13248