Java uses templates to generate Word document template framework based --- Freemarker

Java project introduction Freemarker plug to complete.

 

Proceed as follows:

1, write Word template, and the template data to use to generate code dynamically variable Freemarker substituted with, i.e. variable $ {name}, such as {$ username};

2, put the word document as (as xml file format Select Word 2003 XML, note that Save As, change the extension xml is not ), then change the extension ftl;

3, opened with editing software Final ftl file and find the variable names defined in the first step with a search function, such as: username,

Word software will find in the Word into xml process, $ {username} before inserting the character is too excess (usually with the format in Word), all variable names defined in the first step to restore all is $ {variable name} format, namely: delete $ {username} insertion of extra characters. This step is particularly important to be able to directly determine the correct data to fall behind the template .

FIG follows: between $ {username} is inserted into a lot of formatting characters, it is necessary to remove the string portion of the yellow line.

 

 

 4, the execution code, the variables in the template data is filled into

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class FreeMarkerUtils {
    
    
    
    public static void main(String[] args) {
        try {
            Map<String,String> dataMap = new HashMap<String,String>();
            dataMap.put ( "clinicName", "love oral tooth (Coastal clinics)" );
            dataMap.put("username", "张三");
            dataMap.put("phone", "23456789876");
            dataMap.put("IDNo", "130803299911211789");
            dataMap.put("gender", "男");
            dataMap.put("birthday", "1998-11-21");
            dataMap.put("RightUp", "右上1");
            dataMap.put("LeftUp", "左上1");
            dataMap.put("RightDown", "右下1");
            dataMap.put("LeftDown", "左上1");
            dataMap.put("opearationDate", "2019-11-01");
            dataMap.put ( "amountDaXie", "150.00" );
            dataMap.put("amount", "150.00");
            dataMap.put("currentDate", "2019年11月01日");
            dataMap.put("currentDate", "2019年11月01日");
            
            Configuration configuration = new Configuration();
            configuration.setDefaultEncoding ( "UTF-8" );
             // specify the template path of the second way, my path is D: / There are other ways 
            configuration.setDirectoryForTemplateLoading ( new new File ( "C: / the Users / the User / Desktop" ));
            
            // output file path and name 
            File the outFile = new new File ( "D: /test.doc" );
             // in a utf-8 encoded file read ftl 
            Template t = configuration.getTemplate ( "6_ implant procedure accident insurance mono (. 4) -2003-3.ftl "," UTF-. 8 " );
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"),10240);
            t.process(dataMap, out);
            out.close();
        } catch (IOException e) {
            e.printStackTrace ();
        } catch (Exception e) {
            e.printStackTrace ();
        }
    }
    
}

 

 

 

 

 

reference:

https://www.cnblogs.com/xinde123/p/8581963.html

https://www.cnblogs.com/cnsdhzzl/p/10644597.html

 

Original link:

https://www.cnblogs.com/poterliu/p/11774854.html

 

Guess you like

Origin www.cnblogs.com/poterliu/p/11774854.html