Freemaker使用List标签实现多张图片插入,导出为word

昨天下午因为freemaker插入list图片集合踩了一些坑,因此记录一下希望对大家有用,包含一些注意事项。

1.现将模板转存为xml文件。需要插入信息的地方用freemaker表达式填充,放图片的地方先放一张图片,完成后转存为xml文件

2. 将xml文件保存后,将后缀改为ftl,应为要用到freemaker中的list标签,xml无法显示因此需要将后缀转为ftl

3.将ftl中的图片base64编码的图片信息删除,在<w:binData></w:binData>之间放上图片的freemaker表达式,<#list 图片变量 as 图片别名>后面的根据图片别名做遍历,有点类似foreach。注意事项在图片中已经标记,都是我踩过的坑,希望大家不要踩,晚上其他资料也不全,我这里是全的。

 

4.模板写好了,然后就是写代码了,图片的传入需要将图片装为base64编码,需要base64方法,然后将freemaker导出为word工具方法。

/**
	 *导出为word方法
         * !注意dataMap里存放的数据Key值要与模板中的参数相对应,图片为String类型的Base64编码
	 * 
	 * 载入的数据文件
	 * 
	 * @param dataMap
	 *            模版文件路劲
	 * @param templatePackagePath
	 *            模板文件名称
	 * @param templateXMLName
	 *            输出的的doc地址
	 * @param docPath
	 * 
	 */
	public static void createDoc(Map<String, Object> dataMap, String templatePath, String templateXMLName,
			String docPath) {

		Configuration configuration = new Configuration();
		configuration.setDefaultEncoding("UTF-8");
		Template t = null;

		try {

			FileTemplateLoader templateLoader = new FileTemplateLoader(new File(templatePath));
			configuration.setTemplateLoader(templateLoader);
			t = configuration.getTemplate(templateXMLName, "utf-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 输出文档路径及名称
		File outFile = new File(docPath);
		Writer out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));

		} catch (Exception e1) {
			e1.printStackTrace();

		}

		try {
			t.process(dataMap, out);
		} catch (TemplateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
/**
	 * 该方法用来将指定的文件转换成base64编码
	 * 
	 * @param path:图片路径
	 **/
	private static String getImageStr(String path) {
		// 1、校验是否为空
		if (path == null || path.trim().length() <= 0) {
			return "";
		}

		// 2、校验文件是否为目录或者是否存在
		File picFile = new File(path);
		if (picFile.isDirectory() || (!picFile.exists()))
			return "";

		// 3、校验是否为图片
		try {
			BufferedImage image = ImageIO.read(picFile);
			if (image == null) {
				return "";
			}
		} catch (IOException ex) {
			ex.printStackTrace();
			return "";
		}

		// 4、转换成base64编码
		String imageStr = "";
		try {
			byte[] data = null;
			InputStream in = new FileInputStream(path);
			data = new byte[in.available()];
			in.read(data);
			imageStr = Base64.encodeBase64String(data);
		} catch (Exception e) {
			imageStr = "";
			e.printStackTrace();
		}

 5.使用map插入信息和图片。

		String templatePath = "E:/";
		String templateXMLName = "test.ftl";
		String doctemplatepath = "E:/rwerewres.doc";
		Map<String, Object> map = new HashMap<>();

		String string = WordToPDFUtil.getImageStr("D:/wp/img/23123/4234432/1.jpg");
		String string1 = WordToPDFUtil.getImageStr("D:/wp/img/23123/4234432/2.jpg");
		List<String> list = new ArrayList<>();
		list.add(0, string);
		list.add(0, string1);
		map.put("image", list);

		WordToPDFUtil.createDoc(map, templatePath, templateXMLName, doctemplatepath);

6.最后查看结果,如果是图片不能正常显示可能原因是模板ftl标签写错了,为了防止ftl格式问题最好先压缩一下在用ftl,导出用office不要用wps。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值