Java operations generate Word( poi-tl Implement)
Preface
There's a recent need to generate Word reports, to count the data and present it as Word. Some implementation techniques have been found online, including apache poi and FreeMarker, but both are slightly inadequate for generating Word. apache poi provides Java programs for MicrosoftThe function of reading and writing files in Office format is tedious, has high freedom of operation and needs its own design style for easy maintenance. FreeMarker uses the idea of template engine, is simple and easy to use, works well with HTML and XML, but Word is not easy to operate. Finally, poi-tl is found. It is a Word template engine, which uses Word templates and data to create Word documents.Layer encapsulates apache poi to provide easier to use Api operations.
1. Characteristics
poi-tl can support basic Word content processing such as common text, pictures, tables, lists, charts, etc. In addition, there are other code highlighting, Word annotations, bookmarks, anchors, hyperlinks, text boxes, template nesting, user-defined functions (plug-ins), and so on.
The logic of poi-tl is TDO mode. Template + data-model = output
That is, the result is rendered by compiling the template + data model.
See:
Template: Template
Data-model: Data
Output: Output
2. Quick Start
1. Maven Dependency
<dependency> <groupId>com.deepoove</groupId> <artifactId>poi-tl</artifactId> <version>1.10.0</version> </dependency>
Create a new Word template template.docx with content {{var}}
Content within {{}} can be customized
// Template Loading and Rendering XWPFTemplate template = XWPFTemplate.compile("template.docx").render( // data model new HashMap<String, Object>(){{ put("var", "Hi, poi-tl Word template engine"); }}); // Output Document template.writeAndClose(new FileOutputStream("output.docx"));
This completes a simple document rendering.
3. Case Demonstration
Use a case to illustrate how poi-tl is used
1. Let's first create a template, E:\ComTemp\Test Generation word\Template test.docx. This is the path we created.
2. Making Data Models
Look directly at the code
File fileTempl = new File("E:\\ComTemp\\test generation word\\Template test.docx"); // Making a data model HashMap<String, Object> hashMap = new HashMap<>(); // text hashMap.put("title", "Hi, poi-tl Word template engine"); hashMap.put("name", "Sayi"); Hyperlink and anchor text: hashMap.put("link", Texts.of("website").link("http://deepoove.com").create()); hashMap.put("anchor", Texts.of("anchortxt").anchor("appendix1").create()); // Network Picture (Note the possible performance impact of network time on the system) hashMap.put("urlImg", Pictures.ofUrl("http://deepoove.com/images/icecream.png").size(100, 100).create()); // Network Picture (Note the possible performance impact of network time on the system) // form // Table with center row 0 and blue background RowRenderData row0 = Rows.of("Full name", "Education").textColor("FFFFFF") .bgColor("4472C4").center().create(); RowRenderData row1 = Rows.create("Li Si", "doctor"); hashMap.put("table", Tables.create(row0, row1)); // Block has instructions for follow-up hashMap.put("person", true); // Factory Charts Building Graph Model ChartMultiSeriesRenderData chart = Charts .ofMultiSeries("ChartTitle", new String[] { "Chinese", "English" }) .addSeries("countries", new Double[] { 15.0, 6.0 }) .addSeries("speakers", new Double[] { 223.0, 119.0 }) .create(); hashMap.put("barChart", chart);
3. Execute Rendering Output Document
// Load Template Rendering Data XWPFTemplate template = XWPFTemplate.compile(fileTempl).render(hashMap); // Output Results template.write(new FileOutputStream("E:\\ComTemp\\test generation word\\output.docx")); template.close();
Get results
Explain
Use {{@...} for rendering pictures
Table: {{#...}
Block pairs:
Block pairs can contain multiple pictures, tables, paragraphs, lists, charts, etc. between the start and end tags. Data is displayed according to the Boolean logic of the block pairs. There are several types:
- False or empty collection
Hide all document elements in a block
- Not False and Not Collection
Display document elements in blocks, rendered once
- Non-empty set
Circular rendering of document elements in blocks, depending on the size of the collection
Chart:
How is a document's graph associated with a data template? The answer is to specify a label name in the text selection.
The above describes the basic use of poi-tl. In fact, poi-tl has many ways to manipulate word depending on your needs. More actions are required to view official documents, including text styles, table styles, plug-in functions, EL expressions, etc., which are highly recommended. Write thoroughly.