使用freemarker导出word文档

目前,在导出word文档这方面java有很多解决方案:Jacob、Apache POI、Java2Word、iText等等等等···
今天所要记录的是个人认为比较简单易用,且可以满足一般文档得基本需求得的基于freemarker的word文档导出

word2003之后,加入了将word文件存为xml格式的功能,本文采用的导出方式正是在这个基础之上做的。

首先你需要一个freemarker.jar
然后。。。。核心内容我就在一个类里面都搞定了。。。具体每一步得注释基本标明

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import sun.misc.BASE64Encoder;

/**
 * 使用freemark生成word文件
 *
 */
public class CreateWord {

    /**
     * freemark模板配置
     */
    private Configuration configuration;

    public static void main(String[] args){
        String templatePath = "template/";
        String templateName = "temp.ftl";
        String filePath = "bin/";
        String fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+"ee.doc";
        new CreateWord().create(templatePath,templateName,filePath,fileName);
    }

    public void configure(String templatePath){
        configuration = new Configuration();
        configuration.setDefaultEncoding("utf-8");
        configuration.setClassForTemplateLoading(this.getClass(), templatePath);
    }

    public void create(String templatePath,String templateName,String filePath,String fileName){
        this.configure(templatePath);
        Template template = null;
        try {
            //读取freemarker的导出模板  
            template = configuration.getTemplate(templateName);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //创建一个file对象
        File outFile = new File(filePath+fileName);
        //写入字符流的抽象类
        Writer out = null;
        try {
            //创建一个使用默认大小输出缓冲区的缓冲字符输出流
            //OutputStreamWriter 字节-字符转换流
            //FileOutputStream 文件输出流 
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        Map<String, Object> map = new HashMap<String, Object>();
        //填充数据模型,数据模型就是一个Map对象
        map.put("name", "于心");
        map.put("photo", this.getImageStr());
        map.put("sex", "男");
        map.put("birth", "1992-10");
        map.put("tel", "1234567890");
        map.put("email", "123@163.com");
        map.put("address", "山东 济南");

        try {
            //执行模板,使用提供的数据模型
            template.process(map, out);
            out.close();
        } catch (TemplateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 由于word中图片为base64编码,
     * 故将图片转化为base64编码后的字符串
     * @return String
     */
    public  String getImageStr() {
        String imgFile = new File(this.getClass().getResource("/").getPath()) +"/com/freemarker/doc/template/1.jpg";
        InputStream in = null;
        byte[] data = null;
        try {
            //创建照片的字节输入流
            in = new FileInputStream(imgFile);
            //创建一个长度为照片总大小的内存空间
            data = new byte[in.available()];
            //从输入流中读取文件大小的字节,并将其存储在缓冲区数组 data中
            in.read(data);
            //关闭输入流
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        BASE64Encoder encoder = new BASE64Encoder();
        //进行base64加密
        return encoder.encode(data);
    }
}

然后你需要使用Microsoft Word创建一个Word文件,把你的内容写入这个文件,例:
这里写图片描述

然后,你把这个文件另存为xml格式
再然后呢,把xml中需要动态显示的数据改成${xxx}
后缀改为.ftl
然后就可以愉快导出动态数据的文件了!!!

项目源码:http://git.oschina.net/yuruixin/freemarker_wordFile/tree/master

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值