使用freemarker模板生成带图片的word--xml格式
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                使用freemarker模板生成带图片的word--xml格式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                文章目錄
- 前言
- 一、制作freemarker模板
- 1、 準備模板
- 2、修改模板
 
- 二、后臺代碼
- 1.引入freemarker依賴
- 2. freemarker工具類方法
- 3. 測試方法
 
- 三、測試結果
- 總結
前言
以xml的格式生成word,可以將圖片的二進制數據保存在word中,無需考慮圖片的存放位置。
一、制作freemarker模板
1、 準備模板
首先,新建一個docx,然后往word里粘貼一個圖片,再將word另存為xml文件。
 新建一個freemarker文件,如 word.ftl,將xml內容拷貝到word.ftl中。
2、修改模板
1、找到圖片所在的Relationship ,修改成如下格式
<#list pictList as pict><Relationship Id="rId${pict_index+10}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"Target="media/image${pict_index+10}.png"/> </#list>2、找到 <w:body>,寫入圖片的title和引用的Relationship
 (1)title在
(2)Relationship 在
<a:blip r:embed="rId${pict_index+10}"/>(3)完整的body如下
<w:body><#list pictList as pict ><w:p><w:r><w:t>${pict.title}</w:t></w:r><w:r><w:rPr><w:rFonts w:hint="eastAsia"/></w:rPr><w:t>:</w:t></w:r></w:p><w:p><w:r><w:drawing><wp:inline distT="0" distB="0" distL="0" distR="0"><wp:extent cx="5274310" cy="2743835"/><wp:effectExtent l="0" t="0" r="2540" b="0"/><wp:docPr id="${pict_index+10}" name="圖片 ${pict_index+10}"/><wp:cNvGraphicFramePr><a:graphicFrameLocksxmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"noChangeAspect="1"/></wp:cNvGraphicFramePr><a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"><a:graphicDatauri="http://schemas.openxmlformats.org/drawingml/2006/picture"><pic:picxmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"><pic:nvPicPr><pic:cNvPr id="${pict_index+10}" name="圖片 ${pict_index+10}"/><pic:cNvPicPr><a:picLocks noChangeAspect="1"/></pic:cNvPicPr></pic:nvPicPr><pic:blipFill><a:blip r:embed="rId${pict_index+10}"/><a:stretch><a:fillRect/></a:stretch></pic:blipFill><pic:spPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="5274310" cy="2743835"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom></pic:spPr></pic:pic></a:graphicData></a:graphic></wp:inline></w:drawing></w:r></w:p><w:p/></#list>3、找到 pkg:binaryData標簽
 將原來的二進制數據刪除,并修改為如下格式
二、后臺代碼
1.引入freemarker依賴
<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker --> <dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.28</version> </dependency>2. freemarker工具類方法
public static File crateWord(Map<String, Object> data, String templateName, String targetFile) throws IOException, TemplateException {Configuration cfg = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);String path = templateName.substring(0,templateName.lastIndexOf("/"));String filename = templateName.substring(templateName.lastIndexOf("/") + 1);cfg.setClassForTemplateLoading(TemplateUtils.class, path);cfg.setEncoding(Locale.getDefault(), "UTF-8");cfg.setObjectWrapper(new DefaultObjectWrapper(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS));Writer out = null;File outFile = new File(targetFile);try {Template template = cfg.getTemplate(filename, "UTF-8");out = new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8");template.process(data, out);out.flush();} finally {try {if (out != null) {out.close();}} catch (IOException e) {e.printStackTrace();}}return outFile;}3. 測試方法
public static void main(String[] args) throws Exception {Map data = getData();String templateName = "/templates/ftl/word.ftl";String targetFile = "C:\\Users\\x\\Desktop\\word\\word.docx";TemplateUtils.crateWord(data,templateName,targetFile);}createWord方法
private static Map getData() throws IOException {Map data = new HashMap();List<Map> pictList = new ArrayList<>();Map pict1 = new HashMap();pict1.put("title", "圖片1");pict1.put("data", TemplateUtils.getBinaryData(bathPath + "test2.jpg"));Map pict2 = new HashMap();pict2.put("title", "圖片2");pict2.put("data", TemplateUtils.getBinaryData(bathPath + "test3.jpg"));pictList.add(pict1);pictList.add(pict2);data.put("pictList", pictList);return data;}getBinaryData 方法
public static String getBinaryData(String src) throws IOException {File file = new File(src);InputStream in = new FileInputStream(file);byte[] data = new byte[in.available()];try {in.read(data);} finally {if (in != null) {try {in.close();} catch (IOException e) {e.printStackTrace();}}}Base64.Encoder encoder = Base64.getEncoder();return encoder.encodeToString(data);}三、測試結果
總結
本文介紹了以xml的方式往word里插入圖片。總結
以上是生活随笔為你收集整理的使用freemarker模板生成带图片的word--xml格式的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 前人挖坑,后人往里跳
- 下一篇: 二级分销小程序怎么做
