java 模版式的 word

...

package com.kingzheng.projects.word;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

public class CreateWordTest { 
    // 参考 https://blog.csdn.net/chimaocai1302/article/details/100856467
    
    private Configuration configuration = null;
     
    public CreateWordTest() {
        configuration = new Configuration();
        configuration.setDefaultEncoding("utf-8");
    }
 
    /**@param dataMap 要填充的数据集合 * 
     * @param fileName 生成的word文档路劲与名称 *   Template
     * @throws UnsupportedEncodingException */
    public void createDoc(Map<String,Object> dataMap,String fileName) throws UnsupportedEncodingException {
        System.out.println(dataMap);
        //读取模板   // /sdst4/src/com/jz/dzst/mail/zs_f.ftl
        configuration.setClassForTemplateLoading(this.getClass(), "/com/jz/dzst/mail");
        /* https://www.cnblogs.com/fangjian0423/p/freemarker-templateloading-question.html
        Freemarker提供了3种加载模板目录的方法。 它使用Configuration类加载模板
            public void setClassForTemplateLoading(Class clazz, String pathPrefix);
            public void setDirectoryForTemplateLoading(File dir) throws IOException;
            public void setServletContextForTemplateLoading(Object servletContext, String path);
        */
        Template t=null;
        try {
            t = configuration.getTemplate("zs_f.ftl");
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        File outFile = new File(fileName);
        Writer out = null;
        FileOutputStream fos=null;
        try {
            fos = new FileOutputStream(outFile);
            //注意对流的编码
            OutputStreamWriter oWriter = new OutputStreamWriter(fos,"UTF-8");
            out = new BufferedWriter(oWriter); 
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
 
        try {
            t.process(dataMap, out);
            out.close();
            fos.close();
        } catch (TemplateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        CreateWordTest createWordTest =new CreateWordTest();
        // 太长 附件会变成  .bin 文件
         Map<String, Object> dataMap = new HashMap<String, Object>();
         dataMap.put("xmbh", "1");
         dataMap.put("gcmc", "如何使用freemarker生成word文档");
         dataMap.put("jsdw", "我");
         dataMap.put("nr", "首先建立ftl模板,引入freemarker的jar包,编写MDoc类读取模板,编写Main测试类,使用map集合填充模板");
         dataMap.put("bh", "1");  
         try {
             createWordTest.createDoc(dataMap, "F:/cs.doc");
         } catch (UnsupportedEncodingException e) {
             e.printStackTrace();
         }
    }

}

 

posted @ 2021-02-09 17:12  SilentKiller  阅读(183)  评论(0)    收藏  举报