开发者

How do I get an Ampersand symbol into Word 2007 .docx XML file?

开发者 https://www.devze.com 2023-01-14 20:42 出处:网络
I am generating a Word doc in xml based on customer input, and of course it blows up whenever an & is used. I tried replacing all instances of & with &, but then & literally sh

I am generating a Word doc in xml based on customer input, and of course it blows up whenever an & is used. I tried replacing all instances of & with &, but then & literally shows up in my Word Doc. Here's my code:

        static String replac开发者_JS百科e(String in) {
            String ampersand = "&(?![a-zA-Z][a-zA-Z][a-zA-Z]?[a-zA-Z]?;)";
            return in.replaceAll(ampersand,"&");
        }

Any ideas?


The problem is that the ampersand is escaped differently in TOC and the title.

In TOC, it is escaped as & while in the title is escaped as \u0026.

Solution is to escape ampersand differently in TOC and title:

  • TOC: html escaping sequence
  • TITLE: unicode escape sequence.


Have you tried replacing the ampersand with & or & (unicode)?


Word 2007 itself does &, so your problem must be something else.


Your regular expressions seems wrong to me. Why don't you just do:

    static String replace(String in) {
        return in.replace("&","&");
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消