Freemarker如何遍历对象里的图片

一、做freemarker的模版

        用wps写出模版后转为ftl格式,比如我的模版是这样:

然后点击另存为,一定要另存为xml格式!

二、把此xml文件后缀更改为ftl

其实都一样,改为ftl是为了一目了然大家都知道这是使用freemarker做的,放在template目录下,比如我的文件是test.ftl!

三、修改ftl里图片格式

1、ftl文件里一共有三个地方需要修改,首先根据/relationships/image搜索全文,找到对应的Relationship,把这个ID和Target改为索引加随便一个数字,因为不让他们的id重复,否则会覆盖图片,踩过坑!

2、第2个需要修改的地方是搜索 图片,会出现<wp:docPr这个标签,此标签是用于在word中插入图片的,把id改为event_index加跟上面relationship对应的数字。

3.最后一点就是遍历图片了,把ftl里<pkg:binaryData></pkg:binaryData>中间的内容全部删掉,里面是图片的base64格式,换成自己变量名。

换成${}这个格式的变量名,且去遍历这个对象的属性!

四.生成word文档java代码如下:

private static void createDocFile(Map<String, Object> data, String ftlName, String srcPath) {
try {
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
configuration.setClassForTemplateLoading(EventDetailService.class, "/templates");
Template template = configuration.getTemplate(ftlName, "UTF-8");
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(srcPath), "UTF-8"));
template.process(data, bufferedWriter);
bufferedWriter.flush();
bufferedWriter.close();
} catch (Exception e) {
logger.info("导出文件失败", e);
}
}

//图片转为base64格式
public static String encodeFileToBase64(File file) {
try (FileInputStream fileInputStream = new FileInputStream(file)) {
byte[] bytes = new byte[(int) file.length()];
fileInputStream.read(bytes);
return Base64.getEncoder().encodeToString(bytes);
} catch (IOException e) {
e.printStackTrace();
return "";
}
}

测试主方法:

public static void main(String[] args) {
File file = new File("/Users/wangmeng/Downloads/R-C.png");
String pictureBase = encodeFileToBase64(file);

File file2 = new File("/Users/wangmeng/Downloads/100r.jpg");
String pictureBaseTwo = encodeFileToBase64(file2);
String srcPath = "/Users/wangmeng/logs";
String docFilePath = PathUtils.joinPath(srcPath, UUID.randomUUID().toString() + ".doc");
Map<String, Object> data = new HashMap<>();
List<EventDetail> eventDetailList = new ArrayList<>();
EventDetail eventDetail = new EventDetail();
eventDetail.setName("你好,我是大为1");
eventDetail.setId(12093L);
eventDetail.setPhoneNumber("134532923421");
eventDetail.setSourceType("test");
eventDetail.setScenicImg(pictureBase);
eventDetailList.add(eventDetail);

EventDetail eventDetailTest = new EventDetail();
eventDetailTest.setName("你好,我是小伟2");
eventDetailTest.setId(100L);
eventDetailTest.setPhoneNumber("2324343421");
eventDetailTest.setSourceType("测试类");
eventDetailTest.setScenicImg(pictureBaseTwo);
eventDetailList.add(eventDetailTest);
data.put("eventMap", eventDetailList);
createDocFile(data, "test.ftl", docFilePath);
}

其中EventDetail的属性如下:

private long id;

private String sourceType;

private String phoneNumber;

private String scenicImg;

set和get方法没写,自行补充!

五.导出结果如下:

本人小菜鸟一枚,有表达不对的地方希望各位大佬多多指正,感谢观看!

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值