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