Java 不支持使用各种语言(包括日语、俄语、西班牙语、法语和意大利语)编写 excel 文件的特殊字符
Java is not supporting special characters in various languages including Japanese, Russian, Spanish, French and Italian to write an excel file
我正在从 MySQL 中的数据库读取一些信息,我需要将它们写入 excel 文件
XSSFWorkbook workbook =new XSSFWorkbook();
...
cell.setCellValue(addressValue); // suppose addressValue is what I have obtained from mysql
....
try (FileOutputStream outputStream = new FileOutputStream(myfile)) {
workbook.write(outputStream);
}
在MySQL中,table字符集为UTF8,字段被认为是utf8_general_ci。
但是这种方式,它不处理特殊字符。例如,我看到类似 "名古屋市"
的内容,而它应该是 "名古屋市"
.
或
"Москва"
应该是 "Москва"
.
或
"St. Honoré"
应该是 "St. Honoré"
.
感谢 Mark Rotteveel,我在这里分享了解决我问题的代码,也许它也适用于其他人。 link 是:
how to unescape XML in java
public static String unescapeXML( final String xml )
{
Pattern xmlEntityRegex = Pattern.compile( "&(#?)([^;]+);" );
// Matcher requires a StringBuffer instead of a StringBuilder
StringBuffer unescapedOutput = new StringBuffer( xml.length() );
Matcher m = xmlEntityRegex.matcher( xml );
Map<String,String> builtinEntities = null;
String entity;
String hashmark;
String ent;
int code;
while ( m.find() ) {
ent = m.group(2);
hashmark = m.group(1);
if ( (hashmark != null) && (hashmark.length() > 0) ) {
code = Integer.parseInt( ent );
entity = Character.toString( (char) code );
} else {
//must be a non-numerical entity
if ( builtinEntities == null ) {
builtinEntities = buildBuiltinXMLEntityMap();
}
entity = builtinEntities.get( ent );
if ( entity == null ) {
//not a known entity - ignore it
entity = "&" + ent + ';';
}
}
m.appendReplacement( unescapedOutput, entity );
}
m.appendTail( unescapedOutput );
return unescapedOutput.toString();
}
private static Map<String,String> buildBuiltinXMLEntityMap()
{
Map<String,String> entities = new HashMap<String,String>(10);
entities.put( "lt", "<" );
entities.put( "gt", ">" );
entities.put( "amp", "&" );
entities.put( "apos", "'" );
entities.put( "quot", "\"" );
return entities;
}
我正在从 MySQL 中的数据库读取一些信息,我需要将它们写入 excel 文件
XSSFWorkbook workbook =new XSSFWorkbook();
...
cell.setCellValue(addressValue); // suppose addressValue is what I have obtained from mysql
....
try (FileOutputStream outputStream = new FileOutputStream(myfile)) {
workbook.write(outputStream);
}
在MySQL中,table字符集为UTF8,字段被认为是utf8_general_ci。
但是这种方式,它不处理特殊字符。例如,我看到类似 "名古屋市"
的内容,而它应该是 "名古屋市"
.
或
"Москва"
应该是 "Москва"
.
或
"St. Honoré"
应该是 "St. Honoré"
.
感谢 Mark Rotteveel,我在这里分享了解决我问题的代码,也许它也适用于其他人。 link 是: how to unescape XML in java
public static String unescapeXML( final String xml )
{
Pattern xmlEntityRegex = Pattern.compile( "&(#?)([^;]+);" );
// Matcher requires a StringBuffer instead of a StringBuilder
StringBuffer unescapedOutput = new StringBuffer( xml.length() );
Matcher m = xmlEntityRegex.matcher( xml );
Map<String,String> builtinEntities = null;
String entity;
String hashmark;
String ent;
int code;
while ( m.find() ) {
ent = m.group(2);
hashmark = m.group(1);
if ( (hashmark != null) && (hashmark.length() > 0) ) {
code = Integer.parseInt( ent );
entity = Character.toString( (char) code );
} else {
//must be a non-numerical entity
if ( builtinEntities == null ) {
builtinEntities = buildBuiltinXMLEntityMap();
}
entity = builtinEntities.get( ent );
if ( entity == null ) {
//not a known entity - ignore it
entity = "&" + ent + ';';
}
}
m.appendReplacement( unescapedOutput, entity );
}
m.appendTail( unescapedOutput );
return unescapedOutput.toString();
}
private static Map<String,String> buildBuiltinXMLEntityMap()
{
Map<String,String> entities = new HashMap<String,String>(10);
entities.put( "lt", "<" );
entities.put( "gt", ">" );
entities.put( "amp", "&" );
entities.put( "apos", "'" );
entities.put( "quot", "\"" );
return entities;
}