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