diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 068ab153a..826937bee 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 49273 - Correct handling for Font Character Sets with indicies greater than 127 49334 - Track the ValueRangeRecords of charts in HSSFChart, to allow the basic axis operations 49242 - Track the LinkDataRecords of charts in HSSFChart Improved performance of XSSFWorkbook.write diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFFont.java b/src/java/org/apache/poi/hssf/usermodel/HSSFFont.java index 58621bdc9..4e2a2fe89 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFFont.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFFont.java @@ -280,9 +280,29 @@ public final class HSSFFont implements Font { * @see #DEFAULT_CHARSET * @see #SYMBOL_CHARSET */ - public byte getCharSet() + public int getCharSet() { - return font.getCharset(); + byte charset = font.getCharset(); + if(charset >= 0) { + return (int)charset; + } else { + return charset + 256; + } + } + + /** + * set character-set to use. + * @see #ANSI_CHARSET + * @see #DEFAULT_CHARSET + * @see #SYMBOL_CHARSET + */ + public void setCharSet(int charset) + { + byte cs = (byte)charset; + if(charset > 127) { + cs = (byte)(charset-256); + } + setCharSet(cs); } /** diff --git a/src/java/org/apache/poi/ss/usermodel/Font.java b/src/java/org/apache/poi/ss/usermodel/Font.java index b9c10a8b1..ee0d8add8 100644 --- a/src/java/org/apache/poi/ss/usermodel/Font.java +++ b/src/java/org/apache/poi/ss/usermodel/Font.java @@ -251,7 +251,7 @@ public interface Font { * @see #DEFAULT_CHARSET * @see #SYMBOL_CHARSET */ - byte getCharSet(); + int getCharSet(); /** * set character-set to use. @@ -260,6 +260,13 @@ public interface Font { * @see #SYMBOL_CHARSET */ void setCharSet(byte charset); + /** + * set character-set to use. + * @see #ANSI_CHARSET + * @see #DEFAULT_CHARSET + * @see #SYMBOL_CHARSET + */ + void setCharSet(int charset); /** * get the index within the XSSFWorkbook (sequence within the collection of Font objects) diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java index 7fbbee795..3b92c6fd3 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java @@ -107,13 +107,13 @@ public class XSSFFont implements Font { /** * get character-set to use. * - * @return byte - character-set + * @return int - character-set (0-255) * @see org.apache.poi.ss.usermodel.FontCharset */ - public byte getCharSet() { + public int getCharSet() { CTIntProperty charset = _ctFont.sizeOfCharsetArray() == 0 ? null : _ctFont.getCharsetArray(0); int val = charset == null ? FontCharset.ANSI.getValue() : FontCharset.valueOf(charset.getVal()).getValue(); - return (byte)val; + return val; } @@ -293,6 +293,19 @@ public class XSSFFont implements Font { * @see FontCharset */ public void setCharSet(byte charset) { + int cs = (int)charset; + if(cs < 0) { + cs += 256; + } + setCharSet(cs); + } + /** + * set character-set to use. + * + * @param charset - charset + * @see FontCharset + */ + public void setCharSet(int charset) { CTIntProperty charsetProperty = _ctFont.sizeOfCharsetArray() == 0 ? _ctFont.addNewCharset() : _ctFont.getCharsetArray(0); switch (charset) { case Font.ANSI_CHARSET: diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java index 1bcc98149..fe8d3a588 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java @@ -19,6 +19,7 @@ package org.apache.poi.xssf.usermodel; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.XSSFITestDataProvider; +import org.apache.poi.xssf.XSSFTestDataSamples; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont; @@ -72,6 +73,21 @@ public final class TestXSSFFont extends BaseTestFont{ xssfFont.setCharSet(FontCharset.DEFAULT); assertEquals(FontCharset.DEFAULT.getValue(),ctFont.getCharsetArray(0).getVal()); + + + // Now try with a few sample files + + // Normal charset + XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("Formatting.xlsx"); + assertEquals(0, + workbook.getSheetAt(0).getRow(0).getCell(0).getCellStyle().getFont().getCharSet() + ); + + // GB2312 charact set + workbook = XSSFTestDataSamples.openSampleWorkbook("49273.xlsx"); + assertEquals(134, + workbook.getSheetAt(0).getRow(0).getCell(0).getCellStyle().getFont().getCharSet() + ); } public void testFontName() { diff --git a/test-data/spreadsheet/49273.xlsx b/test-data/spreadsheet/49273.xlsx new file mode 100644 index 000000000..ad1ad278d Binary files /dev/null and b/test-data/spreadsheet/49273.xlsx differ