From 605e028ae56655c3af3a8fe739456ab75b6cebab Mon Sep 17 00:00:00 2001
From: "Andrew C. Oliver"
Rows are created by calling createRow(rowNumber) from an existing instance of HSSFSheet. Only rows that have cell values should be added to the sheet. To set the row's height, you just call @@ -98,18 +102,20 @@ HSSFFont f2 = wb.createFont(); //set font 1 to 12 point type f.setFontHeightInPoints((short) 12); -//make it red -f.setColor((short) HSSFColor.RED.index); +//make it blue +f.setColor( (short)0xc ); // make it bold //arial is the default font -f.setBoldweight(f.BOLDWEIGHT_BOLD); +f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //set font 2 to 10 point type f2.setFontHeightInPoints((short) 10); -//make it the color at palette index 0xf (white) -f2.setColor((short) HSSFColor.WHITE.index); +//make it red +f2.setColor( (short)HSSFFont.COLOR_RED ); //make it bold -f2.setBoldweight(f2.BOLDWEIGHT_BOLD); +f2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); + +f2.setStrikeout( true ); //set cell stlye cs.setFont(f); @@ -120,16 +126,18 @@ cs.setDataFormat(HSSFDataFormat.getFormat("($#,##0_);[Red]($#,##0)")); cs2.setBorderBottom(cs2.BORDER_THIN); //fill w fg fill color cs2.setFillPattern((short) HSSFCellStyle.SOLID_FOREGROUND); -// set foreground fill to red -cs2.setFillForegroundColor((short) HSSFColor.RED.index); // set the font cs2.setFont(f2); -// set the sheet name to HSSF Test -wb.setSheetName(0, "HSSF Test"); -// create a sheet with 300 rows (0-299) -for (rownum = (short) 0; rownum < 300; rownum++) +// set the sheet name in Unicode +wb.setSheetName(0, "\u0422\u0435\u0441\u0442\u043E\u0432\u0430\u044F " + + "\u0421\u0442\u0440\u0430\u043D\u0438\u0447\u043A\u0430", + HSSFWorkbook.ENCODING_UTF_16 ); +// in case of compressed Unicode +// wb.setSheetName(0, "HSSF Test", HSSFWorkbook.ENCODING_COMPRESSED_UNICODE ); +// create a sheet with 30 rows (0-29) +for (rownum = (short) 0; rownum < 30; rownum++) { // create a row r = s.createRow(rownum); @@ -141,8 +149,8 @@ for (rownum = (short) 0; rownum < 300; rownum++) } //r.setRowNum(( short ) rownum); - // create 50 cells (0-49) (the += 2 becomes apparent later - for (short cellnum = (short) 0; cellnum < 50; cellnum += 2) + // create 10 cells (0-9) (the += 2 becomes apparent later + for (short cellnum = (short) 0; cellnum < 10; cellnum += 2) { // create a numeric cell c = r.createCell(cellnum); @@ -151,29 +159,31 @@ for (rownum = (short) 0; rownum < 300; rownum++) + (((double) rownum / 1000) + ((double) cellnum / 10000))); + String cellValue; + + // create a string cell (see why += 2 in the + c = r.createCell((short) (cellnum + 1)); + // on every other row if ((rownum % 2) == 0) { // set this cell to the first cell style we defined c.setCellStyle(cs); + // set the cell's string value to "Test" + c.setEncoding( HSSFCell.ENCODING_COMPRESSED_UNICODE ); + c.setCellValue( "Test" ); } - - // create a string cell (see why += 2 in the - c = r.createCell((short) (cellnum + 1)); - - // set the cell's string value to "TEST" - c.setCellValue("TEST"); - // make this column a bit wider - s.setColumnWidth((short) (cellnum + 1), (short) ((50 * 8) / ((double) 1 / 20))); - - // on every other row - if ((rownum % 2) == 0) + else { - // set this to the white on red cell style - // we defined above c.setCellStyle(cs2); + // set the cell's string value to "\u0422\u0435\u0441\u0442" + c.setEncoding( HSSFCell.ENCODING_UTF_16 ); + c.setCellValue( "\u0422\u0435\u0441\u0442" ); } + + // make this column a bit wider + s.setColumnWidth((short) (cellnum + 1), (short) ((50 * 8) / ((double) 1 / 20))); } } @@ -383,21 +393,21 @@ level API (and indirectly the low level support). The main body of its code is repeated above. To run it:
export HSSFDIR={wherever you put HSSF's jar files}
+ - download the poi-alpha build and untar it (tar xvzf
+ tarball.tar.gz)
+
+ - set up your classpath as follows:
+
export HSSFDIR={wherever you put HSSF's jar files}
export LOG4JDIR={wherever you put LOG4J's jar files}
export CLASSPATH=$CLASSPATH:$HSSFDIR/hssf.jar:$HSSFDIR/poi-poifs.jar:$HSSFDIR/poi-util.jar:$LOG4JDIR/jog4j.jar
- - type:
-
java org.apache.poi.hssf.dev.HSSF ~/myxls.xls write
+
java org.apache.poi.hssf.dev.HSSF ~/myxls.xls write
This should generate a test sheet in your home directory called "myxls.xls"
.
java org.apache.poi.hssf.dev.HSSF ~/input.xls output.xls
+ java org.apache.poi.hssf.dev.HSSF ~/input.xls output.xls
@@ -65,6 +70,7 @@ import org.apache.poi.util.StringUtil; * file.
* REFERENCE: PG 291 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)
* @author Andrew C. Oliver (acoliver at apache dot org)
+ * @author Sergei Kozello (sergeikozello at mail.ru)
* @version 2.0-pre
*/
@@ -116,18 +122,33 @@ public class BoundSheetRecord
throw new RecordFormatException("NOT A Bound Sheet RECORD");
}
}
+
+ /**
+ * UTF8:
+ * sid + len + bof + flags + len(str) + unicode + str
+ * 2 + 2 + 4 + 2 + 1 + 1 + len(str)
+ *
+ * UNICODE:
+ * sid + len + bof + flags + len(str) + unicode + str
+ * 2 + 2 + 4 + 2 + 1 + 1 + 2 * len(str)
+ *
+ */
protected void fillFields(byte [] data, short size, int offset)
{
- field_1_position_of_BOF = LittleEndian.getInt(data,
- 0 + offset);
- field_2_option_flags = LittleEndian.getShort(data,
- 4 + offset);
- field_3_sheetname_length = data[ 6 + offset ];
- field_4_compressed_unicode_flag = data[ 7 + offset ];
- field_5_sheetname = new String(data, 8 + offset,
- LittleEndian.ubyteToInt( field_3_sheetname_length));
- }
+ field_1_position_of_BOF = LittleEndian.getInt(data, 0 + offset); // bof
+ field_2_option_flags = LittleEndian.getShort(data, 4 + offset); // flags
+ field_3_sheetname_length = data[ 6 + offset ]; // len(str)
+ field_4_compressed_unicode_flag = data[ 7 + offset ]; // unicode
+
+ int nameLength = LittleEndian.ubyteToInt( field_3_sheetname_length );
+ if ( ( field_4_compressed_unicode_flag & 0x01 ) == 1 ) {
+ field_5_sheetname = StringUtil.getFromUnicodeHigh( data, 8 + offset, nameLength );
+ }
+ else {
+ field_5_sheetname = new String( data, 8 + offset, nameLength );
+ }
+ }
/**
* set the offset in bytes of the Beginning of File Marker within the HSSF Stream part of the POIFS file
@@ -169,7 +190,7 @@ public class BoundSheetRecord
* @param flag (0/1) 0- compressed, 1 - uncompressed (16-bit)
*/
- public void setCompressedUnicodeFlag(byte flag)
+ public void setCompressedUnicodeFlag( byte flag )
{
field_4_compressed_unicode_flag = flag;
}
@@ -178,8 +199,8 @@ public class BoundSheetRecord
* Set the sheetname for this sheet. (this appears in the tabs at the bottom)
* @param sheetname the name of the sheet
*/
-
- public void setSheetname(String sheetname)
+
+ public void setSheetname( String sheetname )
{
field_5_sheetname = sheetname;
}
@@ -215,7 +236,21 @@ public class BoundSheetRecord
public byte getSheetnameLength()
{
- return field_3_sheetname_length;
+ return field_3_sheetname_length;
+ }
+
+ /**
+ * get the length of the raw sheetname in characters
+ * the length depends on the unicode flag
+ *
+ * @return number of characters in the raw sheet name
+ */
+
+ public byte getRawSheetnameLength()
+ {
+ return (byte)( ( ( field_4_compressed_unicode_flag & 0x01 ) == 1 )
+ ? 2 * field_3_sheetname_length
+ : field_3_sheetname_length );
}
/**
@@ -262,22 +297,47 @@ public class BoundSheetRecord
public int serialize(int offset, byte [] data)
{
LittleEndian.putShort(data, 0 + offset, sid);
- LittleEndian.putShort(data, 2 + offset,
- ( short ) (0x08 + getSheetnameLength()));
+ LittleEndian.putShort( data, 2 + offset, (short)( 8 + getRawSheetnameLength() ) );
LittleEndian.putInt(data, 4 + offset, getPositionOfBof());
LittleEndian.putShort(data, 8 + offset, getOptionFlags());
- data[ 10 + offset ] = getSheetnameLength();
+ data[ 10 + offset ] = (byte)( getSheetnameLength() );
data[ 11 + offset ] = getCompressedUnicodeFlag();
+
+ if ( ( field_4_compressed_unicode_flag & 0x01 ) == 1 )
+ StringUtil.putUncompressedUnicode( getSheetname(), data, 12 + offset );
+ else
+ StringUtil.putCompressedUnicode( getSheetname(), data, 12 + offset );
+
- // we assume compressed unicode (bein the dern americans we are ;-p)
- StringUtil.putCompressedUnicode(getSheetname(), data, 12 + offset);
return getRecordSize();
+
+ /*
+ byte[] fake = new byte[] { (byte)0x85, 0x00, // sid
+ 0x1a, 0x00, // length
+ 0x3C, 0x09, 0x00, 0x00, // bof
+ 0x00, 0x00, // flags
+ 0x09, // len( str )
+ 0x01, // unicode
+ //