java word导出

需要导出的word文档是事先准备好的一个模板,文档数据所在的位置则必须按FreeMarker模板语法的占位符(如:${xxx})填充,然后将word文档保存(最好另存为,原模板也最后保留,便于以后修改)为xml格式的文件,然后使用文本编辑器打开检查并修改不合法或书写更好的FreeMarker语法。最后在后台服务端使用FreeMarker相关的包和类读取模板,返回模板所需的数据变量,输出word文件即可。
导出word功能示例

1、创建word模板

先创建你需要导出具体格式和样式的模板,数据用FreeMarker语法的占位符占据word模板
2、另存并编辑xml文件

创建好word模板之后,需要将word另存为xml格式的文件,这样易于查看和编辑为符合FreeMarker模板语法的xml文件,保证导出word数据的准确性,具体FreeMaker语法网上有很多,这里就不细说了。

import freemarker.template.Configuration;
import freemarker.template.Template;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.Map;

/**
*
*/
public class WordUtil {

private Configuration configuration = null;

/**
* 构造方法
*/
public WordUtil() {
try {
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 根据类路径获取模板
* @param templatePath
* @param templateName
* @return
* @throws IOException
*/
private Template getTemplate(String templatePath, String templateName) throws IOException {
configuration.setDirectoryForTemplateLoading(new File(templatePath));
Template t = configuration.getTemplate(templateName);
t.setEncoding("UTF-8");
return t;
}

/**
* 生成word文档
* @param templatePath
* @param templateName
* @param dataMap
* @param out
*/
public void write(String templatePath, String templateName,
Map<String, Object> dataMap, Writer out) {
try {
Template t = getTemplate(templatePath, templateName);
t.process(dataMap, out);
out.close();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}

}




import com.blinkfox.util.WordUtil;
import com.jfinal.kit.PathKit;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Created by
*/
public class ExportWordTest {

/**
* 构造测试数据
* @return
*/
public static Map<String, Object> createDatas() {
Map<String, Object> testMap = new HashMap<String, Object>();
// 构造散数据
testMap.put("author", "浙江火焰");
testMap.put("date", "2015-11-20");

// 构造列表循环数据存放在ArrayList集合中
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
for (int i = 0; i < 5; i++) {
Map<String, String> map = new HashMap<String, String>();
map.put("xh", (i + 1) + "");
map.put("name", "张三" + i);
map.put("phone", "1381111222" + i);
list.add(map);
}
testMap.put("datas", list);

return testMap;
}

/**
* @param args
*/
public static void main(String[] args) {
Map<String, Object> testMap = createDatas();
WordUtil handler = new WordUtil();
Writer out = null;
try {
// 生成test.doc的word文件到某文件路径下
FileOutputStream fos = new FileOutputStream("/home/blinkfox/文档/test.doc");
out = new OutputStreamWriter(fos, "UTF-8");
String templatePath = PathKit.getRootClassPath() + "/template/";
handler.write(templatePath, "test.xml", testMap, out);
System.out.println("导出成功!");
} catch (Exception e) {
e.printStackTrace();
}
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值