java用freemarker导出数据到word(含多图片)

一、制作word模版

新建word文档,按照需要设置好字体等各种格式;这里为了显得整齐使用了无边框的表格。

将word文档另存为xml文件(注意不是word xml文档,我吃了这家伙的大亏了)

 

然后用文本编辑器打开这个xml文件,将需要动态显示的文字替换为变量,如:${topicName},

图片需要1.将w:binData标签的一堆字符替换成将来包含图片字符的变量2.为了防止生成多图时出错,将v:shape标签的id属性、v:imagedata标签的src属性、w:binData标签的w:name属性替换为变量,这里变量可以像EL表达式一样写在字符串里面,使用形如${var_index}这样的表达式可以获取当前list遍历到的变量索引。

1 <w:pict><w:binData w:name="wordml://${module_index}_${childModule_index}.png">${childModule.src}</w:binData><v:shape id="_x0000_s1026_${module_index}_${childModule_index}" o:spt="75" alt="${childModule.name}" type="#_x0000_t75" style="height:240pt;width:300pt;" filled="f" o:preferrelative="t" stroked="f" coordsize="21600,21600"><v:path/><v:fill on="f" focussize="0,0"/><v:stroke on="f" joinstyle="miter"/><v:imagedata src="wordml://${module_index}_${childModule_index}.png" o:title="${childModule.name}"/><o:lock v:ext="edit" aspectratio="t"/><w10:wrap type="none"/><w10:anchorlock/></v:shape></w:pict>

添加<#list></#list>标签的时候注意标签的位置,看清包含了哪些标签。代码多的快看花眼了(tbl害人啊),使用一个有高亮显示的编辑器何其重要!

搞定后后缀名改为ftl,放到项目中。

二、bean配置,我用了官方文档的最简单配置

1 <!-- freemarkerTemplate -->
2     <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
3        <property name="templateLoaderPath">
4            <value>classpath:/templates/freemarker/</value>
5        </property>
6      </bean>

 

三、controller组织数据

1 @Resource(name="freemarkerConfig") private FreeMarkerConfigurer freemarkerConfig;

这里只贴过来了部分核心代码

1 List<ModuleParam> moduleList = JSONObject.parseArray(json.get("parentList").toString(), ModuleParam.class);
2         String topicName = json.get("topicName").toString();
3         String topicId = json.get("topicId").toString();
4         String summarize = json.get("summarize").toString();
5 
6         Map<String,Object> dataMap = new HashMap<String,Object>();
7         dataMap.put("moduleList", moduleList);
8         dataMap.put("topicName",topicName);
9         dataMap.put("summarizeContent",summarize);
 1 Configuration configuration = freemarkerConfig.getConfiguration();
 2         configuration.setDefaultEncoding("UTF-8");
 3         Template t=null;  
 4         t = configuration.getTemplate("reportTemplate.ftl");  
 5         File outFile = new File(fileName);  
 6         Writer out = null;  
 7         FileOutputStream fos=null;  
 8         try{
 9            fos = new FileOutputStream(outFile);  
10            out = new BufferedWriter(new OutputStreamWriter(fos,"UTF-8"));
11            t.process(dataMap, out);  
12        }finally{
13            if(out != null){
14                out.close();
15            }
16            if(fos != null){
17                fos.close();
18            }
19        }

基本就这些啦~~

参考:

Java用freemarker导出word http://blog.csdn.net/wangqiuyun/article/details/26348819

Java多种方式动态生成doc文档:http://www.cnblogs.com/Joanna-Yan/p/5280272.html

推荐 :freemarker系列

转载于:https://www.cnblogs.com/GoQC/p/5541809.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值