XSSFCell似乎将某些字符序列编码为unicode字符。我如何防止这种情况发生?我需要应用某种字符转义吗?
例如:
cell.setCellValue("LUS_BO_WP_x24B8_AI"); // The cell value now is „LUS_BO_WPⒸAI"在Unicode中,Ⓒ是U+24B8。
我已经尝试过设置ANSI字体并将单元格类型设置为字符串。
发布于 2018-01-12 21:40:49
此字符转换是在XSSFRichTextString.utfDecode()中完成的
我现在已经写了一个函数,基本上是反向做同样的事情。
private static final Pattern utfPtrn = Pattern.compile("_(x[0-9A-F]{4}_)");
private static final String UNICODE_CHARACTER_LOW_LINE = "_x005F_";
public static String escape(final String value) {
if(value == null) return null;
StringBuffer buf = new StringBuffer();
Matcher m = utfPtrn.matcher(value);
int idx = 0;
while(m.find()) {
int pos = m.start();
if( pos > idx) {
buf.append(value.substring(idx, pos));
}
buf.append(UNICODE_CHARACTER_LOW_LINE + m.group(1));
idx = m.end();
}
buf.append(value.substring(idx));
return buf.toString();
}发布于 2021-07-23 14:08:37
根据@matthias-gerth的建议,稍加修改:
XSSFRichTextString类这样的
XSSFRichTextString.setString:st.setT(s); >>XSSFRichTextString.setStringXSSFRichTextString的构造函数:st.setT(str); >> theXSSFRichTextString中添加了这个东西(这非常接近马蒂亚斯的建议):私有静态最终模式模式= Pattern.compile("_xa-fA-F0-9{4}");私有静态最终字符串UNICODE_CHARACTER_LOW_LINE = "_x005F";私有字符串转义(字符串字符串){ if (字符串!=null){ Matcher m=PATTERN.matcher(字符串);if (m.find()) { StringBuffer buf = new StringBuffer();int idx = 0;do { int pos = m.start();if( pos > idx) { buf.append(str.substring( idx,pos));} buf.append(UNICODE_CHARACTER_LOW_LINE + m.group(0));idx= m.end();} while (m.find());buf.append(str.substring(idx));return buf.toString();}} return str;}
https://stackoverflow.com/questions/48222502
复制相似问题