首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Apache POI中的XSSFCell将某些字符序列编码为unicode字符

Apache POI中的XSSFCell将某些字符序列编码为unicode字符
EN

Stack Overflow用户
提问于 2018-01-12 16:45:56
回答 2查看 1.1K关注 0票数 2

XSSFCell似乎将某些字符序列编码为unicode字符。我如何防止这种情况发生?我需要应用某种字符转义吗?

例如:

代码语言:javascript
复制
cell.setCellValue("LUS_BO_WP_x24B8_AI"); // The cell value now is „LUS_BO_WPⒸAI"

在Unicode中,U+24B8

我已经尝试过设置ANSI字体并将单元格类型设置为字符串。

EN

回答 2

Stack Overflow用户

发布于 2018-01-12 21:40:49

此字符转换是在XSSFRichTextString.utfDecode()中完成的

我现在已经写了一个函数,基本上是反向做同样的事情。

代码语言:javascript
复制
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();
}
票数 0
EN

Stack Overflow用户

发布于 2021-07-23 14:08:37

根据@matthias-gerth的建议,稍加修改:

  1. 创建您自己的XSSFRichTextString

这样的

  1. Adapt XSSFRichTextString.setStringst.setT(s); >>XSSFRichTextString.setString

  1. 这样修改XSSFRichTextString的构造函数:st.setT(str); >> the

  1. XSSFRichTextString中添加了这个东西(这非常接近马蒂亚斯的建议):

私有静态最终模式模式= 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;}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48222502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档