springboot 利用html模版导出word

1.maven配置

复制代码
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.32</version>
        </dependency>

2.控制层业务代码

复制代码
@GetMapping("/exportConstructionLogDocx")
@ApiOperation(value = "Ai施工日志导出docx")
public void exportConstructionLogDocx(HttpServletResponse response, @RequestParam(value = "date", required = false) String date) {
    String engId = "HeadUtil.getEngId()";
    try {
        rtAigcService.exportConstructionLogDocx(response, engId, date);
    } catch (Exception e) {
        log.error("Ai施工日志导出docx-异常:", e);
    }
}

3.业务层代码

复制代码
private Configuration configuration = new Configuration(Configuration.VERSION_2_3_31);
复制代码
@Override
public void exportConstructionLogDocx(HttpServletResponse response, String engId, String date) {

    ClassPathResource resource = new         ClassPathResource("/templates/constructionLog.ftl");
    try {
        File templatesFile = resource.getFile();
        configuration.setDirectoryForTemplateLoading(templatesFile.getParentFile());
        Template template = configuration.getTemplate(templatesFile.getName());
        Map<String, String> data = new HashMap<>();
        data.put("content", "我是内容");

        data.put("testHtml","<table class='MsoTableGrid' border='1'>" +
                "    <tr style='height:14.7000pt;'>" +
                "        <td width='128' valign='center'>'" +
                "              测试httml'" +
                "        </td>'" +
                "        <td width='128' valign='center'>'" +
                "           <img src = '如果有图片这里放:图片的base64'/>" +
                "        </td>'" +
                "    </tr>'" +
                "</table>");

        StringBuffer fileName = new StringBuffer();
        fileName.append("施工日志-");
        fileName.append(DateUtils.parseDateToStr(DateUtils.YYYYMMDDHHMMSS, new Date()));
        fileName.append(".docx");
        String enc = "UTF-8";
        response.setCharacterEncoding(enc);
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName.toString(), enc));
        try (Writer writer = response.getWriter()) {
            template.process(data, writer);
        }
    } catch (Exception e) {
        log.error("施工日志-导出失败-异常:", e);
        throw new RuntimeException("施工日志导出失败");
    }
}

4.模版constructionLog.ftl内容

复制代码
<p class="MsoNormal" align="center" style="text-align:center;">
    <span style="mso-spacerun:'yes';font-family:宋体;mso-ascii-font-family:Calibri;
    mso-hansi-font-family:Calibri;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;
    mso-font-kerning:1.0000pt;">
       <font face="宋体">
          施工日志-${content}
       </font>
    </span>
    <span style="mso-spacerun:'yes';font-family:宋体;mso-ascii-font-family:Calibri;
    mso-hansi-font-family:Calibri;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;
    mso-font-kerning:1.0000pt;">
       <o:p></o:p>
    </span>
</p>
${testHtml}
相关推荐
漫随流水1 小时前
旅游推荐系统(login.html)
前端·html·旅游
李白的粉1 小时前
基于springboot的知识管理系统
java·spring boot·毕业设计·课程设计·知识管理系统·源代码
E_ICEBLUE1 小时前
在 Python 中对比 Word 文档:自动生成修订报告
python·word
三水不滴2 小时前
Elasticsearch 实战系列(二):SpringBoot 集成 Elasticsearch,从 0 到 1 实现商品搜索系统
经验分享·spring boot·笔记·后端·elasticsearch·搜索引擎
大模型RAG和Agent技术实践2 小时前
破译Word文档的“语义黑盒”:企业级DOCX RAG架构演进与全链路实战(完整源代码)
人工智能·架构·大模型·word·智能问答·rag
QQ24391972 小时前
spring boot医院挂号就诊系统信息管理系统源码-SpringBoot后端+Vue前端+MySQL【可直接运行】
前端·vue.js·spring boot
Coder-coco2 小时前
家政服务管理系统|基于springboot + vue家政服务管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·家政服务管理系统
张道宁2 小时前
基于Spring Boot与Docker的YOLOv8检测服务实战
spring boot·yolo·docker
学亮编程手记2 小时前
Mars-Admin 基于Spring Boot 3 + Vue 3 + UniApp的企业级管理系统
vue.js·spring boot·uni-app
数据服务生3 小时前
五子棋-html版本
前端·html