java利用Freemarker模板生成docx格式的word文档(全过程)

您所在的位置:网站首页 java生成word模板带图片 java利用Freemarker模板生成docx格式的word文档(全过程)

java利用Freemarker模板生成docx格式的word文档(全过程)

2024-07-16 02:39| 来源: 网络整理| 查看: 265

参考:https://my.oschina.net/u/3737136/blog/2958421?tdsourcetag=s_pcqq_aiomsg

具体思路

 

1.创建一个docx文档模板,其中的英文是根据自己需要填充的内容。

 

 

 

 

 

 

 

2.把docx文档修改为ZIP格式(修改.docx后缀名为.zip),然后把zip解压到当前目录

3.修改word目录下document.xml文档,把如下原来是time改为${time},改好后放入项目中,用于后边内容填充。【有时候一个单词可能会被拆分,自己要做相应的调整】

 

 

   可用于循环填充,相当于 for (Minute minte:minuteList),示列如下:

${minute.meeting_decision_content} 交由 ${minute.meeting_decision_executor} 负责,在 ${minute.meeting_decision_deadline} 前完成。

---------下边思路在代码中实现------------

4..把内容填充到document.xml里5.在输入docx文档的时候把填充过内容的的 document.xml用流的方式写入zip(详见下面代码)。6.输出docx文档docx模板修改成zip格式后的信息如下(因为word文档本身就是ZIP格式实现的)

注:【在文档里边加入图片须进行rels文档的相关操作,我这里没用到,如有需要可以查看上边的参考链接】

 

 

---------代码部分------------

 

pom中导入依赖:

org.freemarker freemarker

 

 

项目结构:【箭头指向才是用到的,其他部分的测试目前没有删】

 

 

FreeMarkUtils类 import freemarker.template.Configuration; import freemarker.template.Template; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.ByteArrayInputStream; import java.io.StringWriter; import java.util.Map; public class FreeMarkUtils { private static Logger logger = LoggerFactory.getLogger(FreeMarkUtils.class); public static Configuration getConfiguration(){ //创建配置实例 Configuration configuration = new Configuration(Configuration.VERSION_2_3_28); //设置编码 configuration.setDefaultEncoding("utf-8"); configuration.setClassForTemplateLoading(FreeMarkUtils.class, "/templates/wordTemplates");//换成自己对应的目录 return configuration; } /** * 获取模板字符串输入流 * @param dataMap 参数 * @param templateName 模板名称 * @return */ public static ByteArrayInputStream getFreemarkerContentInputStream(Map dataMap, String templateName) { ByteArrayInputStream in = null; try { //获取模板 Template template = getConfiguration().getTemplate(templateName); StringWriter swriter = new StringWriter(); //生成文件 template.process(dataMap, swriter); in = new ByteArrayInputStream(swriter.toString().getBytes("utf-8"));//这里一定要设置utf-8编码 否则导出的word中中文会是乱码 } catch (Exception e) { logger.error("模板生成错误!"); } return in; } } FreemarkerTest类 import com.example.meeting.util.FreeMarkUtils; import java.io.*; import java.util.*; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; public class FreemarkerTest { private static String document="document.xml"; //outputStream 输出流可以自己定义 浏览器或者文件输出流 public static void createDocx(Map dataMap, OutputStream outputStream) { ZipOutputStream zipout = null; try { /*//图片配置文件模板 ByteArrayInputStream documentXmlRelsInput = FreeMarkUtils.getFreemarkerContentInputStream(dataMap, documentXmlRels);*/ //内容模板 ByteArrayInputStream documentInput = FreeMarkUtils.getFreemarkerContentInputStream(dataMap, document); //最初设计的模板 //File docxFile = new File(WordUtils.class.getClassLoader().getResource(template).getPath()); File docxFile = new File("xxxx\\会议纪要.zip");//换成自己的zip路径 if (!docxFile.exists()) { docxFile.createNewFile(); } ZipFile zipFile = new ZipFile(docxFile); Enumeration


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3