java 如何将word 转换为ftl_Java之利用FreeMarker导出Word实例

本文详细介绍了如何使用Java和FreeMarker将Word文档转换为FTL格式,包括文字处理、图片替换、表格处理和字体样式的调整,通过创建模板并进行数据替换,实现了内容的动态导出。
摘要由CSDN通过智能技术生成

开心一笑

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

提出问题

Java中如何利用FreeMarker导出word文档???

解决问题

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 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();

}

}

}

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 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();

}

}

}

结果,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> dataMap = new HashMap<>();

/** 表格数据初始化 **/

List 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();

}

}

}

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;

}

}

结果如下:

![这里写图片描述](http://upload-images.jianshu.io/upload_images/2321678-fe12900dbb9277fd?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

7.word中字体样式调整

这里写图片描述

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

这里写图片描述

2)用同样的方法,把word转变为ftl文件,打开如下

这里写图片描述

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

8.页眉页脚设置

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

读书感悟

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

我们破产了!没关系,我来付钱。

不要困扰于过去,时间会治愈一切。

要想原谅一个人,就是要把心里的某个地方留给他。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值