zoukankan      html  css  js  c++  java
  • 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;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系列

  • 相关阅读:
    错误libvirtError: invalid argument: could not find capabilities for domaintype=kvm
    容器部署ES 和 ES head插件
    squid配置yum源代理服务器
    coredns 1.2.2 反复重启问题
    ansible debugger 模块
    入门篇-contrail-command(对接openstack)All-In-One
    目标文件是什么鬼?
    汇编指令集
    切换GCC编译器版本
    kubernetes-dashboard登录出现forbidden 403
  • 原文地址:https://www.cnblogs.com/GoQC/p/5541809.html
Copyright © 2011-2022 走看看