Java generates Word files based on templates

Source: Internet
Author: User

Original: Java generates Word files based on templates

Source code: Http://www.zuidaima.com/share/1550463664884736.htm

Ava build Word template program steps

1. Copy the Freemarker-2.3.13.jar to the project \web-inf\lib directory

2. Edit Template file

(1) Save the doc file as an XML file, open the XML file in the Eclipse environment, and right-click "source" → "format". Note here that the XML file attribute is UTF-8.

(2) Replace the XML file with ${xxx} where it needs to be replaced

(3) Copy the contents of the XML file into the corresponding FTL file

Attention:

How to handle a table if there is a table in the template, only one header and one table row are left in the word file. Then find the position of the table row in the converted XML file, beginning with "<w:tr ..." and ending with "</w:tr>". Add: < #list Wordbeans as w> after the table row: </#list >

How to handle a picture convert the picture you want to save into a binary string, save it in a variable and replace the binary string in the XML file note that the data stored in the Datamap key value corresponds to the parameters in the template

3. Running Word.java

Execution Result:

Java generates Word files based on templates

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.