Officeexport – Java is based on Apache FreeMarker and follows the concept of template + data model = output. It implements javaBean (data source) and Word export of template as style through a minimalist API, providing the following functions:

  • Output of basic text, text placeholder style is output text style.
  • Single or multiple lines of text and table lines are traversed through the output and can be nested in a loop.
  • A plug-in that provides data processing by adding handlers that can customize any output value, such as date, number, etc. text format conversion for a particular item
  • Image retained style output.

Problems solved and a comparison of existing implementations

The tool is designed to reduce the complexity of exporting Word templates without requiring users to know syntax such as the POI Api and Freemarker.

Comparison of existing schemes:

plan cross-platform Ease of use
Freemarker cross-platform Freemarker’s syntax, XML structure configuration, and complex model export can be tedious
Apache POI Java components, cross-platform It requires mastery of the POIApi, which can be complex to configure for complex styles
officeexport Java components, cross-platform Minimal API, minimal template syntax, template style is output style (template style is adjusted directly through Word)

Quick start

Code sample

1. Adjust the Word template, add placeholders, and convert to the Word 2003 XML document (.xml).

Maven introduces jar packages and exports them through the API

  
   <dependency>
       <groupId>com.github.kmood</groupId>
       <artifactId>officeexport-java</artifactId>
       <version>1.0.1.6 - RELEASE</version>
   </dependency>

Copy the code
  HashMap<String, Object> data = newHashMap<>(); . Prepare data data.put("zxsm",zxsmList);
  data.put("sbsm"."Kmood - Export - Trademark Description");
  
  DocumentProducer dp = new DocumentProducer(ActualModelPath);
  dp.Complie(xmlPath, "filename.xml".true);
  dp.produce(map, ExportFilePath);
Copy the code

Example 1- Text output

The code examples

                   HashMap<String, Object> map = new HashMap<>();
                   map.put("text"."Kmood - Text placeholder output");
                   map.put("text1"."Kmood - Text placeholder output 2");
                   DocumentProducer dp = new DocumentProducer(ActualModelPath);
                   String complie = dp.Complie(xmlPath, "text.xml".true);
                   dp.produce(map, ExportFilePath);
Copy the code

Implementation effect

Example 2- Text looping output

The code examples

        // Prepare data
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<Object> tables = new ArrayList<>();
        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("text"."Kmood - Text placeholder output - loop 1");
        map1.put("text1"."Kmood - Text placeholder output 2- Loop 1");
        tables.add(map1);
        HashMap<String, Object> map2 = new HashMap<>();
        map2.put("text"."Kmood - Text placeholder output - Loop 2");
        map2.put("text1"."Kmood - Text placeholder output 2- Loop 2");
        tables.add(map2);
        map.put("tables",map);
        // Compile the output
        DocumentProducer dp = new DocumentProducer(ActualModelPath);
        String complie = dp.Complie(xmlPath, "textf.xml".true);
        dp.produce(map, ExportFilePath);
Copy the code

Implementation effect

Example 3- Text, table loop output

The code examples

        // Prepare data
       HashMap<String, Object> map = new HashMap<>();
       ArrayList<Object> tables = new ArrayList<>();
       HashMap<String, Object> map1 = new HashMap<>();
       map1.put("text"."Kmood - Text placeholder output - loop 1");
       map1.put("text1"."Kmood - Text placeholder output 2- Loop 1");
       // Table data
       ArrayList<Object> columns1= new ArrayList<>();
       HashMap<String, Object> row = new HashMap<>();
       row.put("column1"."kmood-table1-column1-row1");
       row.put("column2"."kmood-table1-column2-row1");
       columns1.add(row);
       HashMap<String, Object> row2 = new HashMap<>();
       row2.put("column1"."kmood-table1-column1-row2");
       row2.put("column2"."kmood-table1-column2-row2");
       columns1.add(row2);
       map1.put("columns", columns1);
       tables.add(map1);

       HashMap<String, Object> map2 = new HashMap<>();
       map2.put("text"."Kmood - Text placeholder output - Loop 2");
       map2.put("text1"."Kmood - Text placeholder output 2- Loop 2");
       // Table data
       ArrayList<Object> columns2= new ArrayList<>();
       HashMap<String, Object> row3 = new HashMap<>();
       row3.put("column1"."kmood-table2-column1-row1");
       row3.put("column2"."kmood-table2-column2-row1");
       columns2.add(row3);
       HashMap<String, Object> row4 = new HashMap<>();
       row4.put("column1"."kmood-table2-column1-row2");
       row4.put("column2"."kmood-table2-column2-row2");
       columns2.add(row4);
       map2.put("columns", columns2);

       tables.add(map2);
       map.put("tables",tables);
       // Compile the output
       DocumentProducer dp = new DocumentProducer(ActualModelPath);
       String complie = dp.Complie(xmlPath, "table.xml".true);
       dp.produce(map, ExportFilePath);
Copy the code

Implementation effect

Chinese document: https://github.com/kmood/officeexport-java/wiki

Making: * * https://github.com/kmood/officeexport-java