Java之利用FreeMarker导出Word实例

来源:https://blog.csdn.net/huangwenyi1010/article/details/51771551

开心一笑

感冒了很难受,她闷在被窝里给男朋友发短信"我感冒了…"并决定如果对方回答"多喝点水"就一脚踹了他。过一会儿手机振动起来,短信内容:"开门。"……这个大笨蛋!谁让他来的啦!她起身用最快的速度冲去门口,此时手机再次振动,她一手开门一手兴奋的点开:"多呼吸点新鲜空气,运动运动。

视频教程

大家好,我录制的视频《Java之优雅编程之道》已经在CSDN学院发布了,有兴趣的同学可以购买观看,相信大家一定会收获到很多知识的。谢谢大家的支持……

视频地址:http://edu.csdn.net/lecturer/994

提出问题

Java中如何利用FreeMarker导出word文档??? 
简书地址:http://www.jianshu.com/users/d38a3668be58/latest_articles

解决问题

1.先用word准备一个模板,如下图: 
这里写图片描述

2.我们把word文档另存为xml格式的文件,用Notepad++工具打开,一下只截取部分内容. 
这里写图片描述

3.我们开始处理第一个问题:文字处理,Ctrl+F找到XML文档中文字,将“以下省略一万字”替换为${textDeal},保存文件,将文件的后缀改为.ftl,自此模板制作成功。

4.编码实现文本替换:

package com.hwy.test;

import freemarker.template.Configuration;
import freemarker.template.Template;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
/**
 * word导出
 * Created by Ay on 2016/6/27.
 */
public class WordDocExportTest {

    public static void main(String[] args) throws Exception{
        /** 初始化配置文件 **/
        Configuration configuration = new Configuration();
        /** 设置编码 **/
        configuration.setDefaultEncoding("utf-8");
        /** 我的ftl文件是放在D盘的**/
        String fileDirectory = "D:\\";
        /** 加载文件 **/
        configuration.setDirectoryForTemplateLoading(new File(fileDirectory));
        /** 加载模板 **/
        Template template = configuration.getTemplate("FreeMarker中word导出XML.ftl");
        /** 准备数据 **/
        Map<String,String> dataMap = new HashMap<>();
        /** 在ftl文件中有${textDeal}这个标签**/
        dataMap.put("textDeal","一下省略一万字");
        /** 指定输出word文件的路径 **/
        String outFilePath = "D:\\myFreeMarker.doc";
        File docFile = new File(outFilePath);
        FileOutputStream fos = new FileOutputStream(docFile);
        Writer out = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"),10240);
        template.process(dataMap,out);

        if(out != null){
            out.close();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

5.编码实现图片的替换

这里写图片描述

具体代码如下:

package com.hwy.test;
import freemarker.template.Configuration;
import freemarker.template.Template;
import sun.misc.BASE64Encoder;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
/**
 * word导出
 * Created by Ay on 2016/6/27.
 */
public class WordDocExportTest {

    public static void main(String[] args) throws Exception{
        /** 初始化配置文件 **/
        Configuration configuration = new Configuration();
        /** 设置编码 **/
        configuration.setDefaultEncoding("utf-8");
        /** 我的ftl文件是放在D盘的**/
        String fileDirectory = "D:\\";
        /** 加载文件 **/
        configuration.setDirectoryForTemplateLoading(new File(fileDirectory));
        /** 加载模板 **/
        Template template = configuration.getTemplate("FreeMarker中word导出XML.ftl");
        /** 准备数据 **/
        Map<String,String> dataMap = new HashMap<>();

        /** 图片路径 **/
        String imagePath = "D:\\apple.jpg";
        /** 将图片转化为**/
        InputStream in = null;
        byte[] data = null;
        try {
            in = new FileInputStream(imagePath);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(in != null){
                in.close();
            }
        }
        /** 进行base64位编码 **/
        BASE64Encoder encoder = new BASE64Encoder();

        /** 在ftl文件中有${textDeal}这个标签**/
        dataMap.put("textDeal","一下省略一万字");
        /** 图片数据**/
        dataMap.put("myImage",encoder.encode(data));

        /** 指定输出word文件的路径 **/
        String outFilePath = "D:\\myFreeMarker.doc";
        File docFile = new File(outFilePath);
        FileOutputStream fos = new FileOutputStream(docFile);
        Writer out = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"),10240);
        template.process(dataMap,out);

        if(out != null){
            out.close();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

结果,I love you这张图片又出现了:

图片

6.freeMarker表格处理 
1)同样道理,找到表格的位置,替换成freeMarker标签,图片如下: 
这里写图片描述

具体代码:

package com.hwy.test;
import freemarker.template.Configuration;
import freemarker.template.Template;                
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * word导出
 * Created by Ay on 2016/6/27.
 */
public class WordDocExportTest {

    public static void main(String[] args) throws Exception{
        /** 初始化配置文件 **/
        Configuration configuration = new Configuration();
        /** 设置编码 **/
        configuration.setDefaultEncoding("utf-8");
        /** 我的ftl文件是放在D盘的**/
        String fileDirectory = "D:\\";
        /** 加载文件 **/
        configuration.setDirectoryForTemplateLoading(new File(fileDirectory));
        /** 加载模板 **/
        Template template = configuration.getTemplate("FreeMarker中word导出XML.ftl");
        /** 准备数据 **/
        Map<String,List<Student>> dataMap = new HashMap<>();

        /** 表格数据初始化 **/
        List<Student> studentList = new ArrayList<>();
        studentList.add(new Student("100424060","小毅","男","25"));
        studentList.add(new Student("100424030","小兰","女","25"));

        /** 表格数据 studentList和freemarker标签要对应**/
        dataMap.put("studentList",studentList);

        /** 指定输出word文件的路径 **/
        String outFilePath = "D:\\myFreeMarker.doc";
        File docFile = new File(outFilePath);
        FileOutputStream fos = new FileOutputStream(docFile);
        Writer out = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"),10240);
        template.process(dataMap,out);

        if(out != null){
            out.close();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

Student类如下:这里有一个问题,如果Student类写在WordDocExportTest类中,就会出现问题,很奇怪,只能单独另启一个类了。

package com.hwy.test;

/**
 * Created by Ay on 2016/6/29.
 */
public class Student{
    private String id;
    private String name;
    private String sex;
    private String age;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public Student(String id, String name, String sex, String age) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

结果如下:

![这里写图片描述](https://img-blog.csdn.net/20160629135612181)
  • 1
  • 2

7.word中字体样式调整 
这里写图片描述

1)我们把四种的字体“这里是字体样式……”在word中字体样式改为仿宋_gb2312,大小改为30号如下:

这里写图片描述 
2)用同样的方法,把word转变为ftl文件,打开如下

这里写图片描述
所有以后word需要什么的样式,都是在原先word调好样式,在转变为ftl文件即可

8.页眉页脚设置 
1)相同道理,在原先word插入页眉“Hello Freemarker”,另存为xml文件,再后缀为ftl文件即可,既是我们要的文件

读书感悟

来自《我脑海中的橡皮擦》

  • 我们破产了!没关系,我来付钱。
  • 不要困扰于过去,时间会治愈一切。
  • 要想原谅一个人,就是要把心里的某个地方留给他。

其他

如果有带给你一丝丝小快乐,就让快乐继续传递下去,欢迎转载,点赞,顶,欢迎留下宝贵的意见,多谢支持!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值