700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > ftl模板导出word 带多张图片

ftl模板导出word 带多张图片

时间:2021-12-15 20:22:47

相关推荐

ftl模板导出word 带多张图片

一.生成ftl文件

1.创建word 预先放入图片

2.将word文件另存为.xml文件

3.将xml文件后缀名改为ftl。

4.将ftl文件放入编辑器中生成模板代码

主要代码:

遍历图片

<#if imageList?? && (imageList?size) &gt; 0><#list imageList as imageList ><w:r><w:rPr><w:rFonts w:fareast="宋体" w:hint="fareast" /><w:b /><w:lang w:fareast="ZH-CN" /></w:rPr><w:pict><w:binData w:name="wordml://${imageList_index}.jpg">${imageList!''}</w:binData><v:shape id="_x0000_s1026" o:spt="75" alt="${imageList_index}" type="#_x0000_t75"style="height:251.85pt;width:308.65pt;" filled="f" o:preferrelative="t"stroked="f"coordsize="21600,21600"><v:path /><v:fill on="f" focussize="0,0" /><v:stroke on="f" /><v:imagedata src="wordml://${imageList_index}.jpg" o:title="" /><o:lock v:ext="edit" aspectratio="t" /><w10:wrap type="none" /><w10:anchorlock /></v:shape></w:pict></w:r></#list></#if>

二,方法调用

1.将图片转为Base64编码

public static String getImagePathStr(String imgFile) {InputStream in = null;byte[] data = null;try {in = new FileInputStream(imgFile);} catch (FileNotFoundException e) {e.printStackTrace();}try {data = new byte[in.available()];// 注:FileInputStream.available()方法能够从输入流中阻断由下一个方法调用这个输入流中读取的剩余字节数in.read(data);in.close();} catch (IOException e) {e.printStackTrace();}BASE64Encoder encoder = new BASE64Encoder();return encoder.encode(data);}

2.将多个图片包装为集合

public static List<String> getAppImageList(String imgURl){ArrayList<String> arrayList = new ArrayList<>();if(imgURl == null || "".equals(imgURl)){return arrayList;}String[] imgURlList = imgURl.split(",");for (String str:imgURlList) {if(!"".equals(str)){arrayList.add(getImageStr(str));}}return arrayList;}

3.将集合添加到map中 给模板循环用

String enclosure = dto.getEnclosure();//将图片 放置list集合中在word中遍历展示 候占国dataMap.put("imageList", BusinessUtils.getAppImageList(enclosure));

4.下载

WordPrint.downLoad(dataMap, "测试.doc", "测试.ftl", request, response);

5.下载方法

public static void downLoad(Map dataMap, String fileName, String tempName,HttpServletRequest request, HttpServletResponse response)throws Exception {String outFilePath = createWord(dataMap, fileName, tempName, request);File file = new File(outFilePath);String realFileName = file.getName();realFileName = new String(realFileName.getBytes("GBK"), "ISO-8859-1");try {InputStream fis = new FileInputStream(file);byte[] buffer = new byte[fis.available()];fis.read(buffer);fis.close();response.reset();response.setContentType("application/file");response.setHeader("Content-disposition", "inline;filename=\""+ realFileName + "\";");response.addHeader("Content-Length", "" + file.length());OutputStream toClient = new BufferedOutputStream(response.getOutputStream());toClient.write(buffer);toClient.flush();toClient.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {file.delete();}}

示例:

希望能帮到你哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。