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

(-)src/documentation/xdocs/faq.xml (+31 lines)
Lines 158-161 Link Here
158
            We will probably enhance HSSF in the future to make this process easier.
158
            We will probably enhance HSSF in the future to make this process easier.
159
        </answer>
159
        </answer>
160
    </faq>
160
    </faq>
161
    <faq>
162
        <question>
163
            I tried to set cell values and Excel sheet name on my native language, 
164
            but I failed to do it. :(
165
        </question>
166
        <answer>
167
            By default HSSF uses cell values and sheet names as compressed unicode, 
168
            so to support localization you should use Unicode. 
169
            To do it you should set it manually:
170
            <source>
171
172
                //
173
                // for sheet name
174
                //
175
                HSSFWorkbook wb = new HSSFWorkbook();
176
                HSSFSheet s = wb.createSheet();
177
                wb.setSheetName( 0, "SomeUnicodeName", HSSFWorkbook.ENCODING_UTF_16 );
178
179
180
                //
181
                // for cell value
182
                //
183
                HSSFRow r = s.createRow( 0 );
184
                HSSFCell c = r.createCell( (short)0 );
185
                c.setCellType( HSSFCell.CELL_TYPE_STRING );
186
                c.setEncoding( HSSFCell.ENCODING_UTF_16 );
187
                c.setCellValue( "\u0422\u0435\u0441\u0442\u043E\u0432\u0430\u044F" );
188
189
            </source>
190
        </answer>
191
    </faq>
161
</faqs>
192
</faqs>
(-)src/documentation/xdocs/hssf/how-to.xml (-37 / +47 lines)
Lines 7-12 Link Here
7
        <authors>
7
        <authors>
8
            <person email="acoliver2@users.sourceforge.net" name="Andrew C. Oliver" id="AO"/>
8
            <person email="acoliver2@users.sourceforge.net" name="Andrew C. Oliver" id="AO"/>
9
            <person email="glens@apache.org" name="Glen Stampoultzis" id="GJS"/>
9
            <person email="glens@apache.org" name="Glen Stampoultzis" id="GJS"/>
10
            <person email="sergeikozello@mail.ru" name="Sergei Kozello" id="SK"/>
10
        </authors>
11
        </authors>
11
    </header>
12
    </header>
12
  <body>
13
  <body>
Lines 42-48 Link Here
42
                    sequence to the workbook. Sheets do not in themselves have a sheet
43
                    sequence to the workbook. Sheets do not in themselves have a sheet
43
                    name (the tab at the bottom); you set
44
                    name (the tab at the bottom); you set
44
                    the name associated with a sheet by calling
45
                    the name associated with a sheet by calling
45
                    HSSFWorkbook.setSheetName(sheetindex,&quot;SheetName&quot;).</p>
46
                    HSSFWorkbook.setSheetName(sheetindex,&quot;SheetName&quot;,encoding).
47
                    The name may be in 8bit format (HSSFWorkbook.ENCODING_COMPRESSED_UNICODE)
48
                    or Unicode (HSSFWorkbook.ENCODING_UTF_16). Default encoding is 8bit per char.
49
                    </p>
46
                    <p>Rows are created by calling createRow(rowNumber) from an existing
50
                    <p>Rows are created by calling createRow(rowNumber) from an existing
47
                    instance of HSSFSheet. Only rows that have cell values should be
51
                    instance of HSSFSheet. Only rows that have cell values should be
48
                    added to the sheet. To set the row's height, you just call
52
                    added to the sheet. To set the row's height, you just call
Lines 98-115 Link Here
98
102
99
//set font 1 to 12 point type
103
//set font 1 to 12 point type
100
f.setFontHeightInPoints((short) 12);
104
f.setFontHeightInPoints((short) 12);
101
//make it red
105
//make it blue
102
f.setColor((short) HSSFColor.RED.index);
106
f.setColor( (short)0xc );
103
// make it bold
107
// make it bold
104
//arial is the default font
108
//arial is the default font
105
f.setBoldweight(f.BOLDWEIGHT_BOLD);
109
f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
106
110
107
//set font 2 to 10 point type
111
//set font 2 to 10 point type
108
f2.setFontHeightInPoints((short) 10);
112
f2.setFontHeightInPoints((short) 10);
109
//make it the color at palette index 0xf (white)
113
//make it red
110
f2.setColor((short) HSSFColor.WHITE.index);
114
f2.setColor( (short)HSSFFont.COLOR_RED );
111
//make it bold
115
//make it bold
112
f2.setBoldweight(f2.BOLDWEIGHT_BOLD);
116
f2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
117
118
f2.setStrikeout( true );
113
119
114
//set cell stlye
120
//set cell stlye
115
cs.setFont(f);
121
cs.setFont(f);
Lines 120-135 Link Here
120
cs2.setBorderBottom(cs2.BORDER_THIN);
126
cs2.setBorderBottom(cs2.BORDER_THIN);
121
//fill w fg fill color
127
//fill w fg fill color
122
cs2.setFillPattern((short) HSSFCellStyle.SOLID_FOREGROUND);
128
cs2.setFillPattern((short) HSSFCellStyle.SOLID_FOREGROUND);
123
// set foreground fill to red
124
cs2.setFillForegroundColor((short) HSSFColor.RED.index);
125
129
126
// set the font
130
// set the font
127
cs2.setFont(f2);
131
cs2.setFont(f2);
128
132
129
// set the sheet name to HSSF Test
133
// set the sheet name in Unicode
130
wb.setSheetName(0, "HSSF Test");
134
wb.setSheetName(0, "\u0422\u0435\u0441\u0442\u043E\u0432\u0430\u044F " + 
131
// create a sheet with 300 rows (0-299)
135
                   "\u0421\u0442\u0440\u0430\u043D\u0438\u0447\u043A\u0430", 
132
for (rownum = (short) 0; rownum < 300; rownum++)
136
                HSSFWorkbook.ENCODING_UTF_16 );
137
// in case of compressed Unicode
138
// wb.setSheetName(0, "HSSF Test", HSSFWorkbook.ENCODING_COMPRESSED_UNICODE );
139
// create a sheet with 30 rows (0-29)
140
for (rownum = (short) 0; rownum < 30; rownum++)
133
{
141
{
134
    // create a row
142
    // create a row
135
    r = s.createRow(rownum);
143
    r = s.createRow(rownum);
Lines 141-148 Link Here
141
    }
149
    }
142
150
143
    //r.setRowNum(( short ) rownum);
151
    //r.setRowNum(( short ) rownum);
144
    // create 50 cells (0-49) (the += 2 becomes apparent later
152
    // create 10 cells (0-9) (the += 2 becomes apparent later
145
    for (short cellnum = (short) 0; cellnum < 50; cellnum += 2)
153
    for (short cellnum = (short) 0; cellnum < 10; cellnum += 2)
146
    {
154
    {
147
        // create a numeric cell
155
        // create a numeric cell
148
        c = r.createCell(cellnum);
156
        c = r.createCell(cellnum);
Lines 151-179 Link Here
151
                + (((double) rownum / 1000)
159
                + (((double) rownum / 1000)
152
                + ((double) cellnum / 10000)));
160
                + ((double) cellnum / 10000)));
153
161
162
        String cellValue;
163
164
        // create a string cell (see why += 2 in the
165
        c = r.createCell((short) (cellnum + 1));
166
        
154
        // on every other row
167
        // on every other row
155
        if ((rownum % 2) == 0)
168
        if ((rownum % 2) == 0)
156
        {
169
        {
157
            // set this cell to the first cell style we defined
170
            // set this cell to the first cell style we defined
158
            c.setCellStyle(cs);
171
            c.setCellStyle(cs);
172
            // set the cell's string value to "Test"
173
            c.setEncoding( HSSFCell.ENCODING_COMPRESSED_UNICODE );
174
            c.setCellValue( "Test" );
159
        }
175
        }
160
176
        else
161
        // create a string cell (see why += 2 in the
162
        c = r.createCell((short) (cellnum + 1));
163
164
        // set the cell's string value to "TEST"
165
        c.setCellValue("TEST");
166
        // make this column a bit wider
167
        s.setColumnWidth((short) (cellnum + 1), (short) ((50 * 8) / ((double) 1 / 20)));
168
169
        // on every other row
170
        if ((rownum % 2) == 0)
171
        {
177
        {
172
            // set this to the white on red cell style
173
            // we defined above
174
            c.setCellStyle(cs2);
178
            c.setCellStyle(cs2);
179
            // set the cell's string value to "\u0422\u0435\u0441\u0442"
180
            c.setEncoding( HSSFCell.ENCODING_UTF_16 );
181
            c.setCellValue( "\u0422\u0435\u0441\u0442" );
175
        }
182
        }
176
183
184
185
        // make this column a bit wider
186
        s.setColumnWidth((short) (cellnum + 1), (short) ((50 * 8) / ((double) 1 / 20)));
177
    }
187
    }
178
}
188
}
179
189
Lines 383-403 Link Here
383
its code is repeated above. To run it:
393
its code is repeated above. To run it:
384
</p>
394
</p>
385
<ul>
395
<ul>
386
	<li>download the poi-alpha build and untar it (tar xvzf
396
    <li>download the poi-alpha build and untar it (tar xvzf
387
	tarball.tar.gz)
397
    tarball.tar.gz)
388
	</li>
398
    </li>
389
	<li>set up your classpath as follows:
399
    <li>set up your classpath as follows:
390
	<code>export HSSFDIR={wherever you put HSSF's jar files}
400
    <code>export HSSFDIR={wherever you put HSSF's jar files}
391
export LOG4JDIR={wherever you put LOG4J's jar files}
401
export LOG4JDIR={wherever you put LOG4J's jar files}
392
export CLASSPATH=$CLASSPATH:$HSSFDIR/hssf.jar:$HSSFDIR/poi-poifs.jar:$HSSFDIR/poi-util.jar:$LOG4JDIR/jog4j.jar</code>
402
export CLASSPATH=$CLASSPATH:$HSSFDIR/hssf.jar:$HSSFDIR/poi-poifs.jar:$HSSFDIR/poi-util.jar:$LOG4JDIR/jog4j.jar</code>
393
	</li><li>type:
403
    </li><li>type:
394
	<code>java org.apache.poi.hssf.dev.HSSF ~/myxls.xls write</code></li>
404
    <code>java org.apache.poi.hssf.dev.HSSF ~/myxls.xls write</code></li>
395
</ul>
405
</ul>
396
<p></p>
406
<p></p>
397
<p>This should generate a test sheet in your home directory called <code>&quot;myxls.xls&quot;</code>.  </p>
407
<p>This should generate a test sheet in your home directory called <code>&quot;myxls.xls&quot;</code>.  </p>
398
<ul>
408
<ul>
399
	<li>Type:
409
    <li>Type:
400
	<code>java org.apache.poi.hssf.dev.HSSF ~/input.xls output.xls</code>
410
    <code>java org.apache.poi.hssf.dev.HSSF ~/input.xls output.xls</code>
401
    <br/>
411
    <br/>
402
    <br/>
412
    <br/>
403
This is the read/write/modify test.  It reads in the spreadsheet, modifies a cell, and writes it back out.
413
This is the read/write/modify test.  It reads in the spreadsheet, modifies a cell, and writes it back out.
(-)src/java/org/apache/poi/hssf/model/Workbook.java (-7 / +25 lines)
Lines 60-65 Link Here
60
import java.util.ArrayList;
60
import java.util.ArrayList;
61
import java.util.List;
61
import java.util.List;
62
import java.util.Iterator;
62
import java.util.Iterator;
63
import java.util.Locale;
63
64
64
65
65
import org.apache.poi.util.POILogger;
66
import org.apache.poi.util.POILogger;
Lines 85-90 Link Here
85
 *
86
 *
86
 * @author  Andrew C. Oliver (acoliver at apache dot org)
87
 * @author  Andrew C. Oliver (acoliver at apache dot org)
87
 * @author  Glen Stampoultzis (glens at apache.org)
88
 * @author  Glen Stampoultzis (glens at apache.org)
89
 * @author  Sergei Kozello (sergeikozello at mail.ru)
88
 * @see org.apache.poi.hssf.usermodel.HSSFWorkbook
90
 * @see org.apache.poi.hssf.usermodel.HSSFWorkbook
89
 * @version 1.0-pre
91
 * @version 1.0-pre
90
 */
92
 */
Lines 410-415 Link Here
410
        return ( BackupRecord ) records.get(backuppos);
412
        return ( BackupRecord ) records.get(backuppos);
411
    }
413
    }
412
    
414
    
415
    
413
    /**
416
    /**
414
     * sets the name for a given sheet.  If the boundsheet record doesn't exist and
417
     * sets the name for a given sheet.  If the boundsheet record doesn't exist and
415
     * its only one more than we have, go ahead and create it.  If its > 1 more than
418
     * its only one more than we have, go ahead and create it.  If its > 1 more than
Lines 419-430 Link Here
419
     * @param sheetname the name for the sheet
422
     * @param sheetname the name for the sheet
420
     */
423
     */
421
    
424
    
422
    public void setSheetName(int sheetnum, String sheetname) {
425
    // for compartibility
426
    public void setSheetName(int sheetnum, String sheetname ) {
427
        setSheetName( sheetnum, sheetname, (byte)0 );
428
    }
429
    
430
    public void setSheetName(int sheetnum, String sheetname, short encoding ) {
423
        checkSheets(sheetnum);
431
        checkSheets(sheetnum);
424
        (( BoundSheetRecord ) boundsheets.get(sheetnum))
432
        
425
        .setSheetname(sheetname);
433
        BoundSheetRecord sheet = (BoundSheetRecord)boundsheets.get( sheetnum );
426
        (( BoundSheetRecord ) boundsheets.get(sheetnum))
434
        sheet.setSheetname(sheetname);
427
        .setSheetnameLength(( byte ) sheetname.length());
435
        sheet.setSheetnameLength( (byte)sheetname.length() );
436
		sheet.setCompressedUnicodeFlag( (byte)encoding );
428
    }
437
    }
429
    
438
    
430
    /**
439
    /**
Lines 1586-1592 Link Here
1586
    }
1595
    }
1587
    
1596
    
1588
    /**
1597
    /**
1589
     * Creates the Country record with the default and current country set to 1
1598
     * Creates the Country record with the default country set to 1
1599
     * and current country set to 7 in case of russian locale ("ru_RU") and 1 otherwise
1590
     * @return record containing a CountryRecord
1600
     * @return record containing a CountryRecord
1591
     * @see org.apache.poi.hssf.record.CountryRecord
1601
     * @see org.apache.poi.hssf.record.CountryRecord
1592
     * @see org.apache.poi.hssf.record.Record
1602
     * @see org.apache.poi.hssf.record.Record
Lines 1596-1602 Link Here
1596
        CountryRecord retval = new CountryRecord();
1606
        CountryRecord retval = new CountryRecord();
1597
        
1607
        
1598
        retval.setDefaultCountry(( short ) 1);
1608
        retval.setDefaultCountry(( short ) 1);
1599
        retval.setCurrentCountry(( short ) 1);
1609
        
1610
        // from Russia with love ;)
1611
        if ( Locale.getDefault().toString().equals( "ru_RU" ) ) {
1612
	        retval.setCurrentCountry(( short ) 7);
1613
        }
1614
        else {
1615
	        retval.setCurrentCountry(( short ) 1);
1616
        }
1617
        
1600
        return retval;
1618
        return retval;
1601
    }
1619
    }
1602
    
1620
    
(-)src/java/org/apache/poi/hssf/record/BoundSheetRecord.java (-20 / +80 lines)
Lines 55-62 Link Here
55
55
56
package org.apache.poi.hssf.record;
56
package org.apache.poi.hssf.record;
57
57
58
import java.io.*;
59
import java.io.UnsupportedEncodingException;
60
61
import org.apache.poi.util.BinaryTree;
58
import org.apache.poi.util.LittleEndian;
62
import org.apache.poi.util.LittleEndian;
59
import org.apache.poi.util.StringUtil;
63
import org.apache.poi.util.StringUtil;
64
import sun.awt.image.ByteInterleavedRaster;
60
65
61
/**
66
/**
62
 * Title:        Bound Sheet Record (aka BundleSheet) <P>
67
 * Title:        Bound Sheet Record (aka BundleSheet) <P>
Lines 65-70 Link Here
65
 *               file. <P>
70
 *               file. <P>
66
 * REFERENCE:  PG 291 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
71
 * REFERENCE:  PG 291 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
67
 * @author Andrew C. Oliver (acoliver at apache dot org)
72
 * @author Andrew C. Oliver (acoliver at apache dot org)
73
 * @author Sergei Kozello (sergeikozello at mail.ru)
68
 * @version 2.0-pre
74
 * @version 2.0-pre
69
 */
75
 */
70
76
Lines 116-133 Link Here
116
            throw new RecordFormatException("NOT A Bound Sheet RECORD");
122
            throw new RecordFormatException("NOT A Bound Sheet RECORD");
117
        }
123
        }
118
    }
124
    }
125
    
126
    /**
127
     *  UTF8:
128
     *	sid + len + bof + flags + len(str) + unicode +   str
129
	 *	 2  +  2  +  4  +   2   +    1     +    1    + len(str)
130
	 * 
131
	 * 	UNICODE:
132
     *	sid + len + bof + flags + len(str) + unicode +   str
133
	 *	 2  +  2  +  4  +   2   +    1     +    1    + 2 * len(str)
134
	 * 
135
     */
119
136
120
    protected void fillFields(byte [] data, short size, int offset)
137
    protected void fillFields(byte [] data, short size, int offset)
121
    {
138
    {
122
        field_1_position_of_BOF         = LittleEndian.getInt(data,
139
        field_1_position_of_BOF         = LittleEndian.getInt(data, 0 + offset);	// bof
123
                0 + offset);
140
        field_2_option_flags            = LittleEndian.getShort(data, 4 + offset);	// flags
124
        field_2_option_flags            = LittleEndian.getShort(data,
141
        field_3_sheetname_length        = data[ 6 + offset ];						// len(str)
125
                4 + offset);
142
        field_4_compressed_unicode_flag = data[ 7 + offset ];						// unicode
126
        field_3_sheetname_length        = data[ 6 + offset ];
143
127
        field_4_compressed_unicode_flag = data[ 7 + offset ];
144
		int nameLength = LittleEndian.ubyteToInt( field_3_sheetname_length );
128
        field_5_sheetname               = new String(data, 8 + offset,
145
        if ( ( field_4_compressed_unicode_flag & 0x01 ) == 1 ) {
129
                LittleEndian.ubyteToInt( field_3_sheetname_length));
146
			field_5_sheetname = StringUtil.getFromUnicodeHigh( data, 8 + offset, nameLength );
130
    }
147
        }
148
        else {
149
			field_5_sheetname = new String( data, 8 + offset, nameLength );
150
        }
151
	}
131
152
132
    /**
153
    /**
133
     * set the offset in bytes of the Beginning of File Marker within the HSSF Stream part of the POIFS file
154
     * set the offset in bytes of the Beginning of File Marker within the HSSF Stream part of the POIFS file
Lines 169-175 Link Here
169
     * @param flag (0/1) 0- compressed, 1 - uncompressed (16-bit)
190
     * @param flag (0/1) 0- compressed, 1 - uncompressed (16-bit)
170
     */
191
     */
171
192
172
    public void setCompressedUnicodeFlag(byte flag)
193
    public void setCompressedUnicodeFlag( byte flag )
173
    {
194
    {
174
        field_4_compressed_unicode_flag = flag;
195
        field_4_compressed_unicode_flag = flag;
175
    }
196
    }
Lines 178-185 Link Here
178
     * Set the sheetname for this sheet.  (this appears in the tabs at the bottom)
199
     * Set the sheetname for this sheet.  (this appears in the tabs at the bottom)
179
     * @param sheetname the name of the sheet
200
     * @param sheetname the name of the sheet
180
     */
201
     */
181
202
    
182
    public void setSheetname(String sheetname)
203
    public void setSheetname( String sheetname )
183
    {
204
    {
184
        field_5_sheetname = sheetname;
205
        field_5_sheetname = sheetname;
185
    }
206
    }
Lines 215-221 Link Here
215
236
216
    public byte getSheetnameLength()
237
    public byte getSheetnameLength()
217
    {
238
    {
218
        return field_3_sheetname_length;
239
		return field_3_sheetname_length;
240
    }
241
242
    /**
243
     * get the length of the raw sheetname in characters
244
     * the length depends on the unicode flag
245
     * 
246
     * @return number of characters in the raw sheet name
247
     */
248
249
    public byte getRawSheetnameLength()
250
    {
251
		return (byte)( ( ( field_4_compressed_unicode_flag & 0x01 ) == 1 )
252
						? 2 * field_3_sheetname_length
253
						: field_3_sheetname_length );
219
    }
254
    }
220
255
221
    /**
256
    /**
Lines 262-283 Link Here
262
    public int serialize(int offset, byte [] data)
297
    public int serialize(int offset, byte [] data)
263
    {
298
    {
264
        LittleEndian.putShort(data, 0 + offset, sid);
299
        LittleEndian.putShort(data, 0 + offset, sid);
265
        LittleEndian.putShort(data, 2 + offset,
300
        LittleEndian.putShort( data, 2 + offset, (short)( 8 + getRawSheetnameLength() ) );
266
                              ( short ) (0x08 + getSheetnameLength()));
267
        LittleEndian.putInt(data, 4 + offset, getPositionOfBof());
301
        LittleEndian.putInt(data, 4 + offset, getPositionOfBof());
268
        LittleEndian.putShort(data, 8 + offset, getOptionFlags());
302
        LittleEndian.putShort(data, 8 + offset, getOptionFlags());
269
        data[ 10 + offset ] = getSheetnameLength();
303
        data[ 10 + offset ] = (byte)( getSheetnameLength() );
270
        data[ 11 + offset ] = getCompressedUnicodeFlag();
304
        data[ 11 + offset ] = getCompressedUnicodeFlag();
305
        
306
        if ( ( field_4_compressed_unicode_flag & 0x01 ) == 1 )
307
	        StringUtil.putUncompressedUnicode( getSheetname(), data, 12 + offset );
308
	    else
309
	        StringUtil.putCompressedUnicode( getSheetname(), data, 12 + offset );
310
		
271
311
272
        // we assume compressed unicode (bein the dern americans we are ;-p)
273
        StringUtil.putCompressedUnicode(getSheetname(), data, 12 + offset);
274
        return getRecordSize();
312
        return getRecordSize();
313
        
314
		/*
315
		byte[] fake = new byte[] {	(byte)0x85, 0x00, 			// sid
316
		    							0x1a, 0x00, 			// length
317
		    							0x3C, 0x09, 0x00, 0x00, // bof
318
		    							0x00, 0x00, 			// flags
319
		    							0x09, 					// len( str )
320
		    							0x01, 					// unicode
321
		    							// <str>
322
		    							0x21, 0x04, 0x42, 0x04, 0x40, 0x04, 0x30, 0x04, 0x3D, 
323
		    							0x04, 0x38, 0x04, 0x47, 0x04, 0x3A, 0x04, 0x30, 0x04   
324
		    							// </str>
325
		    						};
326
		    						
327
		    						sid + len + bof + flags + len(str) + unicode +   str
328
		    						 2  +  2  +  4  +   2   +    1     +    1    + len(str)
329
		
330
		System.arraycopy( fake, 0, data, offset, fake.length );
331
		
332
		return fake.length;
333
		*/
275
    }
334
    }
276
335
277
    public int getRecordSize()
336
    public int getRecordSize()
278
    {
337
    {
279
        return 12 + getSheetnameLength();
338
        // return 30;
280
    }
339
        return 12 + getRawSheetnameLength();
340
	}
281
341
282
    public short getSid()
342
    public short getSid()
283
    {
343
    {
(-)src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java (-3 / +25 lines)
Lines 70-75 Link Here
70
import java.io.IOException;
70
import java.io.IOException;
71
import java.io.InputStream;
71
import java.io.InputStream;
72
import java.io.OutputStream;
72
import java.io.OutputStream;
73
import java.io.UnsupportedEncodingException;
73
import java.util.ArrayList;
74
import java.util.ArrayList;
74
import java.util.List;
75
import java.util.List;
75
76
Lines 82-87 Link Here
82
 * @see org.apache.poi.hssf.usermodel.HSSFSheet
83
 * @see org.apache.poi.hssf.usermodel.HSSFSheet
83
 * @author  Andrew C. Oliver (acoliver at apache dot org)
84
 * @author  Andrew C. Oliver (acoliver at apache dot org)
84
 * @author  Glen Stampoultzis (glens at apache.org)
85
 * @author  Glen Stampoultzis (glens at apache.org)
86
 * @author  Sergei Kozello (sergeikozello at mail.ru)
85
 * @version 2.0-pre
87
 * @version 2.0-pre
86
 */
88
 */
87
89
Lines 200-206 Link Here
200
202
201
        // none currently
203
        // none currently
202
    }
204
    }
203
205
    
206
    
207
	public final static byte ENCODING_COMPRESSED_UNICODE	= 0;
208
	public final static byte ENCODING_UTF_16 			= 1;
209
	
204
    /**
210
    /**
205
     * set the sheet name.
211
     * set the sheet name.
206
     * @param sheet number (0 based)
212
     * @param sheet number (0 based)
Lines 209-221 Link Here
209
215
210
    public void setSheetName(int sheet, String name)
216
    public void setSheetName(int sheet, String name)
211
    {
217
    {
218
        workbook.setSheetName( sheet, name, ENCODING_COMPRESSED_UNICODE );
219
    }
220
221
    public void setSheetName( int sheet, String name, short encoding )
222
    {
212
        if (sheet > (sheets.size() - 1))
223
        if (sheet > (sheets.size() - 1))
213
        {
224
        {
214
            throw new RuntimeException("Sheet out of bounds");
225
            throw new RuntimeException("Sheet out of bounds");
215
        }
226
        }
216
        workbook.setSheetName(sheet, name);
227
        
228
        switch ( encoding ) {
229
        case ENCODING_COMPRESSED_UNICODE:
230
        case ENCODING_UTF_16:
231
            break;
232
            
233
        default:
234
        	// TODO java.io.UnsupportedEncodingException
235
			throw new RuntimeException( "Unsupported encoding" );
236
        }
237
        
238
        workbook.setSheetName( sheet, name, encoding );
217
    }
239
    }
218
240
    
219
    /**
241
    /**
220
     * get the sheet name
242
     * get the sheet name
221
     * @param sheet Number
243
     * @param sheet Number
(-)src/java/org/apache/poi/util/StringUtil.java (-16 / +41 lines)
Lines 62-69 Link Here
62
62
63
/**
63
/**
64
 *  Title: String Utility Description: Collection of string handling utilities
64
 *  Title: String Utility Description: Collection of string handling utilities
65
 * 
66
 * Now it is quite confusing: the method pairs, in which
67
 * one of them write data and other read written data are:
68
 * putUncompressedUnicodeHigh and getFromUnicode
69
 * putUncompressedUnicode     and getFromUnicodeHigh
65
 *
70
 *
66
 *@author     Andrew C. Oliver
71
 *@author     Andrew C. Oliver
72
 *@author     Sergei Kozello (sergeikozello at mail.ru)
67
 *@created    May 10, 2002
73
 *@created    May 10, 2002
68
 *@version    1.0
74
 *@version    1.0
69
 */
75
 */
Lines 79-84 Link Here
79
     *  given a byte array of 16-bit unicode characters, compress to 8-bit and
85
     *  given a byte array of 16-bit unicode characters, compress to 8-bit and
80
     *  return a string
86
     *  return a string
81
     *
87
     *
88
     * { 0x16, 0x00 } -> 0x16
89
     * 
82
     *@param  string                              the byte array to be converted
90
     *@param  string                              the byte array to be converted
83
     *@param  offset                              the initial offset into the
91
     *@param  offset                              the initial offset into the
84
     *      byte array. it is assumed that string[ offset ] and string[ offset +
92
     *      byte array. it is assumed that string[ offset ] and string[ offset +
Lines 103-124 Link Here
103
        if ((len < 0) || (((string.length - offset) / 2) < len)) {
111
        if ((len < 0) || (((string.length - offset) / 2) < len)) {
104
            throw new IllegalArgumentException("Illegal length");
112
            throw new IllegalArgumentException("Illegal length");
105
        }
113
        }
106
        byte[] bstring = new byte[len];
114
        
107
        int index = offset;
115
        char[] chars = new char[ len ];
108
        // start with high bits.
116
        for ( int i = 0; i < chars.length; i++ ) {
109
117
            chars[i] = (char)( string[ offset + ( 2*i ) ] + 
110
        for (int k = 0; k < len; k++) {
118
                             ( string[ offset + ( 2*i+1 ) ] << 8 ) );
111
            bstring[k] = string[index];
112
            index += 2;
113
        }
119
        }
114
        return new String(bstring);
120
121
        return new String( chars );
115
    }
122
    }
116
    
123
    
117
    
124
    
125
    /**
126
     *  given a byte array of 16-bit unicode characters, compress to 8-bit and
127
     *  return a string
128
     * 
129
     * { 0x16, 0x00 } -> 0x16
130
     *
131
     *@param  string  the byte array to be converted
132
     *@return         the converted string
133
     */
134
135
    public static String getFromUnicodeHigh( final byte[] string ) {
136
        return getFromUnicodeHigh( string, 0, string.length / 2 );
137
    }
138
118
139
119
    /**
140
    /**
120
     *  given a byte array of 16-bit unicode characters, compress to 8-bit and
141
     *  given a byte array of 16-bit unicode characters, compress to 8-bit and
121
     *  return a string
142
     *  return a string
143
     * 
144
     * { 0x00, 0x16 } -> 0x16
122
     *
145
     *
123
     *@param  string                              the byte array to be converted
146
     *@param  string                              the byte array to be converted
124
     *@param  offset                              the initial offset into the
147
     *@param  offset                              the initial offset into the
Lines 144-164 Link Here
144
        if ((len < 0) || (((string.length - offset) / 2) < len)) {
167
        if ((len < 0) || (((string.length - offset) / 2) < len)) {
145
            throw new IllegalArgumentException("Illegal length");
168
            throw new IllegalArgumentException("Illegal length");
146
        }
169
        }
147
        byte[] bstring = new byte[len];
170
148
        int index = offset + 1;
171
        
149
        // start with low bits.
172
        char[] chars = new char[ len ];
150
173
        for ( int i = 0; i < chars.length; i++ ) {
151
        for (int k = 0; k < len; k++) {
174
            chars[i] = (char)( ( string[ offset + ( 2*i ) ] << 8 ) +
152
            bstring[k] = string[index];
175
            					string[ offset + ( 2*i+1 ) ] );
153
            index += 2;
154
        }
176
        }
155
        return new String(bstring);
177
        
178
        return new String( chars );
156
    }
179
    }
157
180
158
181
159
    /**
182
    /**
160
     *  given a byte array of 16-bit unicode characters, compress to 8-bit and
183
     *  given a byte array of 16-bit unicode characters, compress to 8-bit and
161
     *  return a string
184
     *  return a string
185
     * 
186
     * { 0x00, 0x16 } -> 0x16
162
     *
187
     *
163
     *@param  string  the byte array to be converted
188
     *@param  string  the byte array to be converted
164
     *@return         the converted string
189
     *@return         the converted string
(-)src/testcases/org/apache/poi/util/TestStringUtil.java (+43 lines)
Lines 64-69 Link Here
64
 *
64
 *
65
 * @author  Marc Johnson (mjohnson at apache dot org
65
 * @author  Marc Johnson (mjohnson at apache dot org
66
 * @author  Glen Stampoultzis (glens at apache.org)
66
 * @author  Glen Stampoultzis (glens at apache.org)
67
 * @author  Sergei Kozello (sergeikozello at mail.ru)
67
 */
68
 */
68
69
69
public class TestStringUtil
70
public class TestStringUtil
Lines 97-102 Link Here
97
        }
98
        }
98
        assertEquals("abcdefghijklmnop",
99
        assertEquals("abcdefghijklmnop",
99
                     StringUtil.getFromUnicode(test_data));
100
                     StringUtil.getFromUnicode(test_data));
101
    }
102
103
    /**
104
     * test simple form of getFromUnicode with symbols with code below and more 127
105
     */
106
107
    public void testGetFromUnicodeSymbolsWithCodesMoreThan127()
108
    {
109
        byte[] test_data = new byte[] { 	0x04, 0x22,
110
            								0x04, 0x35,
111
            								0x04, 0x41,
112
            								0x04, 0x42,
113
            								0x00, 0x20,
114
            								0x00, 0x74,
115
            								0x00, 0x65,
116
            								0x00, 0x73,
117
            								0x00, 0x74,
118
            								};
119
120
        assertEquals("\u0422\u0435\u0441\u0442 test",
121
                     StringUtil.getFromUnicode(test_data));
122
    }
123
124
    /**
125
     * test getFromUnicodeHigh for symbols with code below and more 127
126
     */
127
128
    public void testGetFromUnicodeHighSymbolsWithCodesMoreThan127()
129
    {
130
        byte[] test_data = new byte[] { 	0x22, 0x04,
131
            								0x35, 0x04,
132
            								0x41, 0x04,
133
            								0x42, 0x04,
134
            								0x20, 0x00,
135
            								0x74, 0x00,
136
            								0x65, 0x00,
137
            								0x73, 0x00,
138
            								0x74, 0x00,
139
            								};
140
141
        assertEquals("\u0422\u0435\u0441\u0442 test",
142
                     StringUtil.getFromUnicodeHigh( test_data ) );
100
    }
143
    }
101
144
102
    /**
145
    /**

Return to bug 10548