freemarker生成word
利用freemarker生成word,在项目中有用到,就单独写个测试以及用法列出来,欢迎圈错,共同学习。
一、应用场景和效果图
1.应用场景:
a.xx项目里面需要定期生成xx报告,记录最近xx情况。
b.需要录入xx信息,由管理人员导出.doc
c.生成word的地方……
2.效果图
a.业务逻辑模板,根据实际应用进行编辑。
模板内容自定义,${xx}为数据库填充内容,由于freemarker与el表达式类似,所以利用${}即可找到需要替换的地方,用中文方便理解
尽量用英文。
b.填充之后的效果图
根据实际需要从数据库读取数据即可。
二、代码实现
1.创建ftl模板,详见效果图步骤
2.实现代码
public void createDoc() throws Exception { System.out.println("正在生成word,请稍后……"); /* 在整个应用的生命周期中,这个工作你应该只做一次。 */ /* 创建和调整配置。 */ Configuration cfg = new Configuration(); cfg.setClassForTemplateLoading(getClass(), "/com/tablemiao/word"); /* 在整个应用的生命周期中,这个工作你可以执行多次 */ /* 获取或创建模板 */ Template temp = cfg.getTemplate("template.ftl"); // 读取图片 Base64 b64Encoder = new Base64(); File file = new File("D:\\wordtest\\tx.jpg"); FileInputStream fis = new FileInputStream(file); byte[] imgData = new byte[fis.available()]; fis.read(imgData); fis.close(); String imgDataStr = b64Encoder.encodeAsString(imgData); /* 创建数据模型 */ //此处便是内容填充(内容可由数据库读取) Map<String, Object> root = new HashMap<String, Object>(); root.put("imgData", imgDataStr); root.put("模板名称", "tablemiaoTest"); root.put("模板编号", "编号89757"); root.put("模板风格", "简简单单的我,简简单单的活"); root.put("模板联系人", "tablemiao"); root.put("模板联系电话", 123456); root.put("模板创建时间", new SimpleDateFormat("yyyy年MM月dd日").format(new Date())); Calendar c = Calendar.getInstance(); c.add(Calendar.DAY_OF_MONTH, 1); root.put("模板完成时间", new SimpleDateFormat("yyyy年MM月dd日").format(c.getTime())); root.put("模板历时", "1天"); root.put("模伴简述", "测试功能模板,以实际需求为准!"); //读取第二张图片 多张图片依次处理 File fileTwo = new File("D:\\wordtest\\bz.jpg"); FileInputStream fisTwo = new FileInputStream(fileTwo); byte[] imgDataTwo = new byte[fisTwo.available()]; fisTwo.read(imgDataTwo); fisTwo.close(); String imgDataStrTwo = b64Encoder.encodeAsString(imgDataTwo); root.put("desc",imgDataStrTwo.trim()); /* 将模板和数据模型合并 */ // 输出文档路径及名称 File outFile = new File("D:/wordtest/tx.doc"); Writer out = null; try { out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8")); } catch (FileNotFoundException e1) { e1.printStackTrace(); } temp.process(root, out); out.flush(); out.close(); System.out.println("word创建完成!"); }
三、附件有源码,加入相应图片,从新配置模板,即可
相关推荐
freemarker 生成word,支持一个单元格生成多张图片
FreeMarker生成word,比较简单
利用FreeMarker生成word文件例子
解决用freemarker生成word时如何插入多张图片
java+Freemarker生成word
java利用freemarker生成word.pdf
freemarker根据模板ftl生成word,pdf格式转word,pdf指定位置插入图片
SpringBoot_Freemarker生成Word_多个表格+两层嵌套循环; 步骤说明: 1.用Microsoft Office Word打开word原件;将文档中需要动态生成的内容,替换为属性名 ${name} 2.另存为,选择保存类型Word 2003 XML 文档(*....
Springboot+freemarker生成word,通过运行wordUtil里面main函数查看结果,包含图片显示等
freemarker生成word文档模板,配合文档使用,效果更佳。https://blog.csdn.net/xueshuiyy/article/details/86748009
freemarker 生成word
Java freemarker生成word文件的例子
转载的文章,利用freemarker生成word,可适用于(有附表的或无附表)word模板填充动态数据。
关于Freemarker生成word的使用(java生成word).pdf
使用freemarker生成word ,并集成struts2 同时生成及下载文档 资料附有Java源代码和自己总结的使用说明及注意事项 大至预览如下: 1、用word编辑好模板 普通字符串替换为 ${string} 表格循环用标签 姓名:${...
freemarker生成word文件 1.模板制造 先用word编辑好格式样式。 另存为xml。 直接将后缀改为ftl格式,模板就生成了。 2.freemarker文件中的运算判断方式,可自行查询百度