* @param sheetnum the sheet number (0 based)
* @param sheetname the name for the sheet
*/
-
- // for compatibility
public void setSheetName(int sheetnum, String sheetname ) {
- setSheetName( sheetnum, sheetname, (byte)0 );
+ checkSheets(sheetnum);
+ BoundSheetRecord sheet = (BoundSheetRecord)boundsheets.get( sheetnum );
+ sheet.setSheetname(sheetname);
+ sheet.setSheetnameLength( (byte)sheetname.length() );
}
/**
return false;
}
+ /**
+ * sets the name for a given sheet forcing the encoding. This is STILL A BAD IDEA.
+ * Poi now automatically detects unicode
+ *
+ *@deprecated 3-Jan-06 Simply use setSheetNam e(int sheetnum, String sheetname)
+ * @param sheetnum the sheet number (0 based)
+ * @param sheetname the name for the sheet
+ */
public void setSheetName(int sheetnum, String sheetname, short encoding ) {
checkSheets(sheetnum);
BoundSheetRecord sheet = (BoundSheetRecord)boundsheets.get( sheetnum );
if (value.length() >255) {
throw new IllegalArgumentException("String literals in formulas cant be bigger than 255 characters ASCII");
}
- this.field_2_options=0;
- this.fHighByte.setBoolean(field_2_options, false);
+ this.field_2_options=0;
+ field_2_options = (byte)this.fHighByte.setBoolean(field_2_options, StringUtil.hasMultibyte(value));
this.field_3_string=value;
this.field_1_length=value.length(); //for the moment, we support only ASCII strings in formulas we create
}
int sst = 0;
UnicodeString str = getRichStringCellValue().getUnicodeString();
- if (encoding == ENCODING_COMPRESSED_UNICODE)
- {
- str.setCompressedUnicode();
- } else if (encoding == ENCODING_UTF_16)
- {
- str.setUncompressedUnicode();
- }
+//jmh if (encoding == ENCODING_COMPRESSED_UNICODE)
+//jmh {
+// jmh str.setCompressedUnicode();
+// jmh } else if (encoding == ENCODING_UTF_16)
+// jmh {
+// jmh str.setUncompressedUnicode();
+// jmh }
sst = book.addSSTString(str);
lrec.setSSTIndex(sst);
getRichStringCellValue().setUnicodeString(book.getSSTString(sst));
int index = 0;
UnicodeString str = value.getUnicodeString();
- if (encoding == ENCODING_COMPRESSED_UNICODE)
- {
- str.setCompressedUnicode();
- } else if (encoding == ENCODING_UTF_16)
- {
- str.setUncompressedUnicode();
- }
+// jmh if (encoding == ENCODING_COMPRESSED_UNICODE)
+// jmh {
+// jmh str.setCompressedUnicode();
+// jmh } else if (encoding == ENCODING_UTF_16)
+// jmh {
+// jmh str.setUncompressedUnicode();
+// jmh }
index = book.addSSTString(str);
(( LabelSSTRecord ) record).setSSTIndex(index);
stringValue = value;
* @see #ENCODING_UTF_16
*
* @return -1, 1 or 0 for unchanged, compressed or uncompressed (used only with String type)
+ *
+ * @deprecated As of 3-Jan-06 POI now automatically handles Unicode without forcing the encoding.
*/
public short getEncoding()
{
* @see #ENCODING_UTF_16
*
* @param encoding either ENCODING_COMPRESSED_UNICODE (0) or ENCODING_UTF_16 (1)
+ * @deprecated As of 3-Jan-06 POI now automatically handles Unicode without forcing the encoding.
*/
public void setEncoding(short encoding)
return workbook.getWindowOne().getDisplayedTab();
}
+ /**
+ * @deprecated POI will now properly handle unicode strings without
+ * forceing an encoding
+ */
public final static byte ENCODING_COMPRESSED_UNICODE = 0;
+ /**
+ * @deprecated POI will now properly handle unicode strings without
+ * forceing an encoding
+ */
public final static byte ENCODING_UTF_16 = 1;
if (workbook.doesContainsSheetName( name, sheet ))
throw new IllegalArgumentException( "The workbook already contains a sheet with this name" );
- workbook.setSheetName( sheet, name, ENCODING_COMPRESSED_UNICODE );
+ if (sheet > (sheets.size() - 1))
+ {
+ throw new RuntimeException("Sheet out of bounds");
+ }
+
+ workbook.setSheetName( sheet, name);
}
+
+ /**
+ * set the sheet name forcing the encoding. Forcing the encoding IS A BAD IDEA!!!
+ * @deprecated 3-Jan-2006 POI now automatically detects unicode and sets the encoding
+ * appropriately. Simply use setSheetName(int sheet, String encoding)
+ * @throws IllegalArgumentException if the name is greater than 31 chars
+ * or contains /\?*[]
+ * @param sheet number (0 based)
+ */
public void setSheetName( int sheet, String name, short encoding )
{
if (workbook.doesContainsSheetName( name, sheet ))
public void testWideRecordLength()
throws Exception
{
- BoundSheetRecord record = new BoundSheetRecord();
- record.setCompressedUnicodeFlag((byte)0x01);
- record.setSheetname("Sheet1");
+ BoundSheetRecord record = new BoundSheetRecord();
+ record.setSheetname("Sheet\u20ac");
record.setSheetnameLength((byte)6);
assertEquals(" 2 + 2 + 4 + 2 + 1 + 1 + len(str) * 2", 24, record.getRecordSize());
try
{
- b.setSheetName( 3, "name1", HSSFWorkbook.ENCODING_UTF_16 );
+ b.setSheetName( 3, "name1"/*JMH, HSSFWorkbook.ENCODING_UTF_16*/ );
fail();
}
catch ( IllegalArgumentException pass )
{
}
- b.setSheetName( 3, "name2", HSSFWorkbook.ENCODING_UTF_16 );
- b.setSheetName( 3, "name2", HSSFWorkbook.ENCODING_UTF_16 );
+ b.setSheetName( 3, "name2"/*JMH, HSSFWorkbook.ENCODING_UTF_16*/ );
+ b.setSheetName( 3, "name2"/*JMH, HSSFWorkbook.ENCODING_UTF_16*/ );
b.setSheetName( 3, "name2" );
HSSFWorkbook c = new HSSFWorkbook( );
--- /dev/null
+package org.apache.poi.hssf.usermodel;\r
+\r
+import java.io.File;\r
+import java.io.FileOutputStream;\r
+import java.io.FileInputStream;\r
+\r
+import org.apache.poi.util.TempFile;\r
+\r
+import junit.framework.TestCase;\r
+\r
+public class TestUnicodeWorkbook extends TestCase {\r
+\r
+ public TestUnicodeWorkbook(String s) {\r
+ super(s);\r
+ }\r
+ \r
+ /** Tests that all of the unicode capable string fields can be set, written and then read back\r
+ * \r
+ *\r
+ */\r
+ public void testUnicodeInAll() throws Exception {\r
+ HSSFWorkbook wb = new HSSFWorkbook();\r
+ //Create a unicode dataformat (contains euro symbol)\r
+ HSSFDataFormat df = wb.createDataFormat();\r
+ final String formatStr = "_([$\u20ac-2]\\\\\\ * #,##0.00_);_([$\u20ac-2]\\\\\\ * \\\\\\(#,##0.00\\\\\\);_([$\u20ac-2]\\\\\\ *\\\"\\-\\\\\"??_);_(@_)";\r
+ short fmt = df.getFormat(formatStr);\r
+ \r
+ //Create a unicode sheet name (euro symbol)\r
+ HSSFSheet s = wb.createSheet("\u20ac");\r
+ \r
+ //Set a unicode header (you guessed it the euro symbol)\r
+ HSSFHeader h = s.getHeader();\r
+ h.setCenter("\u20ac");\r
+ h.setLeft("\u20ac");\r
+ h.setRight("\u20ac");\r
+ \r
+ //Set a unicode footer\r
+ HSSFFooter f = s.getFooter();\r
+ f.setCenter("\u20ac");\r
+ f.setLeft("\u20ac");\r
+ f.setRight("\u20ac"); \r
+\r
+ HSSFRow r = s.createRow(0);\r
+ HSSFCell c = r.createCell((short)1);\r
+ c.setCellValue(12.34);\r
+ c.getCellStyle().setDataFormat(fmt);\r
+ \r
+ HSSFCell c2 = r.createCell((short)2);\r
+ c.setCellValue(new HSSFRichTextString("\u20ac"));\r
+\r
+ HSSFCell c3 = r.createCell((short)3);\r
+ String formulaString = "TEXT(12.34,\"\u20ac###,##\")";\r
+ c3.setCellFormula(formulaString);\r
+\r
+ \r
+ File tempFile = TempFile.createTempFile("unicode", "test.xls");\r
+ FileOutputStream stream = new FileOutputStream(tempFile);\r
+ wb.write(stream);\r
+ \r
+ wb = null;\r
+ FileInputStream in = new FileInputStream(tempFile);\r
+ wb = new HSSFWorkbook(in);\r
+\r
+ //Test the sheetname\r
+ s = wb.getSheet("\u20ac");\r
+ assertNotNull(s);\r
+ \r
+ //Test the header\r
+ h = s.getHeader();\r
+ assertEquals(h.getCenter(), "\u20ac");\r
+ assertEquals(h.getLeft(), "\u20ac");\r
+ assertEquals(h.getRight(), "\u20ac");\r
+ \r
+ //Test the footer\r
+ f = s.getFooter();\r
+ assertEquals(f.getCenter(), "\u20ac");\r
+ assertEquals(f.getLeft(), "\u20ac");\r
+ assertEquals(f.getRight(), "\u20ac"); \r
+\r
+ //Test the dataformat\r
+ r = s.getRow(0);\r
+ c = r.getCell((short)1);\r
+ df = wb.createDataFormat();\r
+ assertEquals(formatStr, df.getFormat(c.getCellStyle().getDataFormat()));\r
+ \r
+ //Test the cell string value\r
+ c2 = r.getCell((short)2);\r
+ assertEquals(c.getRichStringCellValue().getString(), "\u20ac");\r
+ \r
+ //Test the cell formula\r
+ c3 = r.getCell((short)3);\r
+ assertEquals(c3.getCellFormula(), formulaString);\r
+ }\r
+\r
+}\r