Freemarker exports the Word file, and \ n contains invalid issue resolution records

Source: Internet
Author: User

Currently, the developed system uses freemarker to export the image content to Word documents.

The newline \ n is invalid and becomes a space.

First of all, I suspect there is a problem with writing a Word file in Java. I tried the following code and it is normal.

File OUTFILE = new file ("D:/mine.doc ");
Writer out1 = NULL;
Try {
Out1 = new bufferedwriter (New outputstreamwriter (
New fileoutputstream (OUTFILE), "UTF-8 "));
Out1.write ("test1 \ r \ ntest2 ");
Out1.close ();
} Catch (exception e ){
E. printstacktrace ();
}

After reading the freemarker source code for half a day, there is no result.

Later I found that the table is used in the template file. Is this a problem. Sure enough.

The line break (\ r \ n) will be output as a space in the table of the word, but it will be normally displayed in a common word file.

Therefore, \ n is replaced for fields that may wrap in the template file. (It's a bit earthy, but there's no other good way. It's been around for a long time .)

<W: p wsp: rsidr = "003f5617" WSP: rsidrpr = "003f5617" WSP: rsidrdefault = "003f5617" WSP: rsidp = "003f5617">
<W: PPR>
<W: jc w: val = "Left"/>
<W: RPR>
<W: rfonts W: ASCII = "simsun" W: H-ANSI = "simsun"/>
<Wx: font wx: val = "simsun"/>
<W: sz w: val = "22"/>
</W: RPR>
</W: PPR>
<W: r wsp: rsidrpr = "003f5617">
<W: RPR>
<W: rfonts W: ASCII = "simsun" W: H-ANSI = "simsun"/>
<Wx: font wx: val = "simsun"/>
<W: B/>
<W: sz w: val = "22"/>
</W: RPR>
<W: T> responsibility Description: </W: T>
</W: r>
<W: r wsp: rsidr = "004b3f82">
<W: RPR>
<W: rfonts W: ASCII = "simsun" W: Fareast = "" W: H-ANSI = "simsun" W: hint = "Fareast"/>
<Wx: font wx: val = "simsun"/>
<W: sz w: val = "22"/>
</W: RPR>
<W: T >$ {ex. Responsibility? Replace ("\ n", "</W: T> </W: r> </W: P> <W: PPR> <W: RPR> <W: rfonts W: ASCII = \ "simsun \" W: Fareast = \ "\" W: H-ANSI = \ "simsun \" W: hint = \ "Fareast \"/> <wx: font wx: val = \ "simsun \"/> <W: sz w: val = \ "22 \"/> </W: RPR> </W: PPR> <W: r> <W: RPR> <W: rfonts W: ASCII = \ "simsun \"
W: Fareast = \ "\" W: H-ANSI = \ "simsun \" W: hint = \ "Fareast \"/> <wx: font wx: val = \ "simsun \"/> <W: sz w: val = \ "22 \"/> </W: RPR> <W: T> ")} </W: T>
</W: r>
</W: P>

I tried it in office2003 and 2007. OK ~!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.