1.调用free marker导出word

  @Testpublic void testExportWord() {EnergyVo vo = new EnergyVo(); // 为ftl中所用的属性集合对象File file = null;String fileName = "test.ftl";try {file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "ftl/"+fileName);
//            file = new File()getClass().getClassLoader().getResource("lic").getFile();
//            ResourceFinder.getResourceAsStream("");} catch (FileNotFoundException e) {e.printStackTrace();}DocumentHandler documentHandler = new DocumentHandler();//保存路径String savePath = file.getParent()+"\\test4.doc";System.out.println("保存路径:"+savePath);setAllData(vo);
//        setImage(file, vo);documentHandler.createDoc(file.getParent(), file.getName(), savePath, vo);System.out.println("ok");}

1.将test.doc文件另存为test.xml格式的文件。

2.更改test.xml为test.ftl。文中的路径需要自己更改。

3.EnergyVo 为ftl中所用的属性的集合对象,如 属性 String head,在test.ftl中可${head}拿到值。

2.导出word模板

 /**** @param dir 模板加载路径* @param tempName 模板名称* @param savePath  文件保存的路径、文件名* @param sDate 数据集*/public void createDoc(String dir, String tempName, String savePath, Object sDate) {Template template = null;// 文件保存位置File outFile = new File(savePath);Writer out = null;try {// 设置模板加载路径configuration.setDirectoryForTemplateLoading(new File(dir));// 异常控制configuration.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);// 加载模板template = configuration.getTemplate(tempName);//生成文件的路径及文件名FileOutputStream fileOutputStream = new FileOutputStream(outFile);OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "utf-8");BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);out =bufferedWriter;// 合并模板和数据template.process(sDate, out);out.close();} catch (Exception e) {e.printStackTrace();}}

3.test.ftl文件解析

1.表格模板的制作

java类vo中建立属性

 @ApiModelProperty(value = "表1-1")private List<Map<String, Object>> listTab1_1;test类中赋值
List<Map<String, Object>> listTab1_1 = new ArrayList<>();for (int i = 0; i < 4; i++) {Map<String, Object> map = new HashMap<>();map.put("sort", "_");map.put("name", "_");map.put("trans", "_");map.put("sale", "_");listTab1_1.add(map);}vo.setListTab1_1(listTab1_1);

ftl文件中映射取值

注意:属性命名时首字母不能大写。中间不能有特殊符号,如- +,可以为_。

<w:tr>为表格的行,w:t为一个单元格。<w:t>处为单元格的值,<w:tcw >为单元格长度

2. 遇到空单元格。需要合并单元格如何做

可在要遍历此单元格的属性前添加空值判断

注意:边框问题:常见边框标签

<w:tcBorders>
<w:left w:val="single" w:sz="4" w:space="0" w:color="000000"/>
<w:bottom w:val="single" w:sz="4" w:space="0" w:color="000000"/>
<w:right w:val="single" w:sz="4" w:space="0" w:color="000000"/>
</w:tcBorders>可通过这种方法添加边框
方法2.在<w:tcW w:w="1506" w:type="dxa"/>下面可添加此标签
<w:vMerge w:val="restart"/>为有值单元格所有的标签
<w:vMerge/>为空值单元格的标签

3.如何插入图片

根据<v:imagedata r:id="rId8" o:title="1521623111(1)"/> 此处的id值搜索。找到target属性值media/image2.png。搜索此值。找到一段64位编码的符号。在<pkg:binaryData>中。删除这些符号。使用vo中的属性值代替

<pkg:part pkg:name="/word/media/image2.png" pkg:contentType="image/png" pkg:compression="store"><pkg:binaryData> ${image4_1} </pkg:binaryData>
</pkg:part>
String imagepath = file.getParent() + "/image4_1.png";// 图片的路径private static String getImageStr(String imagepath) {InputStream in = null;byte[] data = null;try {in = new FileInputStream(imagepath);data = new byte[in.available()];in.read(data);in.close();BASE64Encoder encoder = new BASE64Encoder();return encoder.encode(data);} catch (IOException e) {System.out.println(e.getMessage());}return null;}

将经过此方法的返回值赋值给vo类中的属性

vo.setImage4_1(getImageStr(imagepath));

到此。图片就可以在模板中显示

以上内容为初次使用freemaker的心得,以后遇到更多问题会持续更新。

freemarker制作word模板相关推荐

  1. 关于使用freemarker制作word模板经历的一些问题

    1.在制作xml模板工具时,千万不要使用wps,千万不要,千万不要,千万不要,wps的普适性更强,其制作的模板极有可能在office中打不开. 2.另存xml时,最好选择2003xml. 3.格式化x ...

  2. springboot+FreeMarker制作word模板

    FreeMarker 是一款 模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页,电子邮件,配置文件,源代码等)的通用工具. 它不是面向最终用户的,而是一个Java类库,是一 ...

  3. freemarker 制作word文档docx案例

    主要用于生成特定word文档,需要替换docx中的文字,书签,图片,动态表格等 本文以生成docx为例,doc文件目前有一些问题 例如图片或模板不好处理等,暂不考虑. 思路: 1)熟悉docx文件的结 ...

  4. freemarker生成word模板

    freemarker生成word模板 一.引入依赖 <dependency><groupId>org.freemarker</groupId><artifac ...

  5. Mht制作word模板

    Mht制作word模板 一.我们把制作好的word样式模板,直接另存为mhtml格式 二.使用记事本打开,尽量不使用Notepad++打开,或者其他编程软件,因为有些字符会被转义,造成各种问题. 三. ...

  6. freemarker 使用word模板赋值

    1. 引包<dependency><groupId>org.freemarker</groupId><artifactId>freemarker< ...

  7. Java实现freemarker生成word模板

    博主在做的是将word源文件中的内容读取出来,然后按照固定的格式输出,当然,源文件有大量,所以编写了程序来批量自动生成目标文件. 本文要介绍的就是如何生成模板,并将从源文件截取的内容填充到模板文件(即 ...

  8. 用freemarker生成word模板

    需求: 给文书统一生成一个搞头文件,文件内容基本是一样的. 用freemarker生成docx文档 一.生成一个docx结尾的word模板,然后把文档的后缀docx改成zip,zip里会有生成很多文件 ...

  9. freemarker根据word模板导出动态word(Excel模板导出Excel)

    1.创建word模板 将需要替换的位置用"${}"代替,如下图: 2.将做好的模板文件保存为xml文件 3.打开xml文件,查看"${unitName}"是否在 ...

  10. Freemarker 生成word 模板文件

    2019独角兽企业重金招聘Python工程师标准>>> /***      * word 下载生成      * @param positionvo      * @return   ...

最新文章

  1. 基于三维点云数据的主成分分析方法(PCA)的python实现
  2. ApacheServer-----关于443端口被占用的解决方法
  3. 驱动编程中的头文件与内核源码的关系
  4. [仙吕·一半儿] 题画《翠林和鸣》
  5. (十七)java版spring cloud+spring boot 社交电子商务平台-spring+springmvc+kafka分布式消息中间件集成方案...
  6. 在Eclipse 2.0中使用版本控制系统CVS
  7. JQuery:deferred对象的方法
  8. matlab与树莓派通信
  9. 网页关闭(解决window.close在火狐下不兼容问题)
  10. 函数栈帧的创建和销毁图解
  11. ZBrush软件特性之Color调控板
  12. javascript中的XML
  13. 用python实现的仿真程序_如何实现疫情扩散仿真程序?
  14. 【紫书第九章】动态规划(DP)常见模型汇总与DP问题分析方法
  15. C语言 编写加密程序,将用户输入的一个英文句子加密为加密字符串,然后输出加密字符串。
  16. 上映4天,票房7.4亿的《海王》,用Python分析数据看大片!
  17. 汽车学习---汽车知识大全【all】
  18. 怎么用计算机给u盘加密文件,win10电脑怎么给U盘文件夹加密
  19. 深度精简版操作系统下IIS的安装方法
  20. sklearn笔记18决策树gini系数

热门文章

  1. C# PDF转图片(JPG,Png)
  2. 这些CAD制图初学入门问题你遇到过吗?CAD制图初学入门全攻略!
  3. [自制]python批量压缩图像
  4. Java SimpleDateFormat 中英文时间格式化转换
  5. 互联网与物联网有什么区别?
  6. synchronized关键词保证线程安全
  7. 红蓝攻防比赛得哪些事
  8. 【论文研读】【目标检测】Revisiting Open World Object Detection
  9. 川土微电子|数字隔离器替代光耦817
  10. python xy 3_python(3):文件操作/os库