java 利用freemaker 导出 word 文档

1:导入maven jar包


			org.freemarker
			freemarker
			2.3.28
		

2:引入FreeMarkerUtil 工具类:

package com.cnten.common.utils;
import freemarker.template.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import sun.misc.BASE64Encoder;

import java.io.*;
import java.util.*;

public class FreeMarkerUtil {

    private final static Logger log = LoggerFactory.getLogger(FreeMarkerUtil.class);
    private static final String ENCODING = "UTF-8";
    private static Configuration cfg = new Configuration(new Version("2.3.28"));

    //初始化cfg
    static {

        //设置模板所在文件夹
        cfg.setClassForTemplateLoading(FreeMarkerUtil.class, "/doc/word");
        // setEncoding这个方法一定要设置国家及其编码,不然在ftl中的中文在生成html后会变成乱码
        cfg.setEncoding(Locale.getDefault(), ENCODING);
        // 设置对象的包装器
        cfg.setObjectWrapper(new DefaultObjectWrapper(new Version("2.3.28")));
        // 设置异常处理器,这样的话就可以${a.b.c.d}即使没有属性也不会出错
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);

    }

    //获取模板对象
    public static Template getTemplate(String templateFileName) throws IOException {
        return cfg.getTemplate(templateFileName, ENCODING);
    }

    /**
     * 据数据及模板生成文件
     *
     * @param data             Map的数据结果集
     * @param templateFileName ftl模版文件名
     * @param outFilePath      生成文件名称(可带路径)
     */
    public static File crateFile(Map data, String templateFileName, String outFilePath) {
        Writer out = null;
        File outFile = new File(outFilePath);
        try {
            // 获取模板,并设置编码方式,这个编码必须要与页面中的编码格式一致
            Template template = getTemplate(templateFileName);
            if (!outFile.getParentFile().exists()) {
                outFile.getParentFile().mkdirs();
            }
            out = new OutputStreamWriter(new FileOutputStream(outFile), ENCODING);
            // 处理模版
            template.process(data, out);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return outFile;
    }

    /**
     * 据数据及模板生成文件
     *
     * @param data             Map的数据结果集
     * @param templateFileName ftl模版文件名
     */
    public static void crateFileInExploer(Map data, String templateFileName, OutputStream outputStream) {
        Writer out = null;
        try {
            // 获取模板,并设置编码方式,这个编码必须要与页面中的编码格式一致
            Template template = getTemplate(templateFileName);
            out = new OutputStreamWriter(outputStream, ENCODING);
            // 处理模版
            template.process(data, out);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    //获得图片的base64码
    public static String getImageBase(String src) throws Exception {
        if (src == null || src == "") {
            return "";
        }
        File file = new File(src);
        if (!file.exists()) {
            return "";
        }
        InputStream in = null;
        byte[] data = null;
        try {
            in = new FileInputStream(file);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }

    public static void main(String[] args) {
        try {
            Map data = new HashMap();
            data.put("name", "段然涛");
            data.put("sex", "男");
            data.put("birthday", "1994-03-14");
            data.put("phone", "17737138812");
            data.put("address", "河南省许昌市");
            data.put("school", "江西科技师范大学");
            List> educations = new ArrayList>();
            Map paramsMap = new HashMap();
            paramsMap.put("school", "禹州一高");
            paramsMap.put("startDate", "2008-09");
            paramsMap.put("endDate", "2012-06");
            paramsMap.put("person", "李磊");
            Map paramsMap2 = new HashMap();
            paramsMap2.put("school", "江西科技师范大学");
            paramsMap2.put("startDate", "2012-09");
            paramsMap2.put("endDate", "2016-07");
            paramsMap2.put("person", "李杰");
            educations.add(paramsMap);
            educations.add(paramsMap2);
            data.put("educations", educations);
            List images = new ArrayList();
            images.add(getImageBase("C:/Users/Administrator/Desktop/图片/timg.jpg"));
            images.add(getImageBase("C:/Users/Administrator/Desktop/图片/timg11.jpg"));
            data.put("images", images);
            crateFile(data, "文档1.ftl", "C:/Users/Administrator/Desktop/文档/简历.doc");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Springmvc 后台导出controller:

@Override
    public void exportReport(String id, HttpServletRequest request, HttpServletResponse response) {
        FfWzxTestInvest ffWzxTestInvest=mapper.selectByPrimaryKey(id);
        Map data = new HashMap();
        data.put("GCBH",ffWzxTestInvest.getInvestProjectNumName());

        // 创建Example
        Example example = new Example(FfWzxTestInvestSon.class);
        // 创建Criteria
        Example.Criteria criteria = example.createCriteria();
        // 添加条件
        criteria.andEqualTo("investId", id);
        List ffWzxTestInvestSonList=ffWzxTestInvestSonMapper.selectByExample(example);
        data.put("son", ffWzxTestInvestSonList);


        response.setHeader("Content-type", "application/msword");
        try {
            response.setHeader("Content-disposition","attachment; filename="+ URLEncoder.encode("调查表.docx","UTF-8"));
            crateFileInExploer(data, "调查表.ftl",response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

你可能感兴趣的:(java 利用freemaker 导出 word 文档)