From 605e028ae56655c3af3a8fe739456ab75b6cebab Mon Sep 17 00:00:00 2001 From: "Andrew C. Oliver" Date: Sun, 21 Jul 2002 03:03:57 +0000 Subject: [PATCH] from russia with love (LOL) http://nagoya.apache.org/bugzilla/show_bug.cgi?id=10548 #1 PR: Obtained from: Submitted by: Reviewed by: git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352795 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/xdocs/faq.xml | 31 ++++++ src/documentation/xdocs/hssf/how-to.xml | 84 ++++++++------- .../org/apache/poi/hssf/model/Workbook.java | 32 ++++-- .../poi/hssf/record/BoundSheetRecord.java | 100 ++++++++++++++---- .../poi/hssf/usermodel/HSSFWorkbook.java | 28 ++++- src/java/org/apache/poi/util/StringUtil.java | 55 +++++++--- .../org/apache/poi/util/TestStringUtil.java | 43 ++++++++ 7 files changed, 291 insertions(+), 82 deletions(-) diff --git a/src/documentation/xdocs/faq.xml b/src/documentation/xdocs/faq.xml index f12a3f0b35..02789bf0ec 100644 --- a/src/documentation/xdocs/faq.xml +++ b/src/documentation/xdocs/faq.xml @@ -158,4 +158,35 @@ We will probably enhance HSSF in the future to make this process easier. + + + I tried to set cell values and Excel sheet name on my native language, + but I failed to do it. :( + + + By default HSSF uses cell values and sheet names as compressed unicode, + so to support localization you should use Unicode. + To do it you should set it manually: + + + // + // for sheet name + // + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet s = wb.createSheet(); + wb.setSheetName( 0, "SomeUnicodeName", HSSFWorkbook.ENCODING_UTF_16 ); + + + // + // for cell value + // + HSSFRow r = s.createRow( 0 ); + HSSFCell c = r.createCell( (short)0 ); + c.setCellType( HSSFCell.CELL_TYPE_STRING ); + c.setEncoding( HSSFCell.ENCODING_UTF_16 ); + c.setCellValue( "\u0422\u0435\u0441\u0442\u043E\u0432\u0430\u044F" ); + + + + diff --git a/src/documentation/xdocs/hssf/how-to.xml b/src/documentation/xdocs/hssf/how-to.xml index 74b3cc45e9..39aa03ca22 100644 --- a/src/documentation/xdocs/hssf/how-to.xml +++ b/src/documentation/xdocs/hssf/how-to.xml @@ -7,6 +7,7 @@ + @@ -42,7 +43,10 @@ sequence to the workbook. Sheets do not in themselves have a sheet name (the tab at the bottom); you set the name associated with a sheet by calling - HSSFWorkbook.setSheetName(sheetindex,"SheetName").

+ HSSFWorkbook.setSheetName(sheetindex,"SheetName",encoding). + The name may be in 8bit format (HSSFWorkbook.ENCODING_COMPRESSED_UNICODE) + or Unicode (HSSFWorkbook.ENCODING_UTF_16). Default encoding is 8bit per char. +

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:

This should generate a test sheet in your home directory called "myxls.xls".