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}
相关推荐
congvee1 小时前
springboot 学习第1期 - 创建工程
spring boot
风流 少年2 小时前
Cursor创建Spring Boot项目
java·spring boot·后端
R-sz2 小时前
导出word并且插入图片
开发语言·c#·word
wáng bēn2 小时前
【java17】使用 Word 模板导出带替换符、动态表格和二维码的文档
java·word·itextpdf
毕设源码_钟学姐2 小时前
计算机毕业设计springboot宿舍管理信息系统 基于Spring Boot的高校宿舍管理平台设计与实现 Spring Boot框架下的宿舍管理系统开发
spring boot·后端·课程设计
军军君012 小时前
基于Springboot+UniApp+Ai实现模拟面试小工具二:后端项目搭建
前端·javascript·spring boot·spring·微信小程序·前端框架·集成学习
全栈凯哥3 小时前
16.Spring Boot 国际化完全指南
java·spring boot·后端
rzl026 小时前
HTML/JOSN复习总结
前端·html
小杨爱学习zb6 小时前
如何将公式图片转换为公式格式到wps/word里面
word
后端小肥肠6 小时前
效率革命!10分钟用Dify+Spring Boot打造AI热点雷达,自媒体选赛道再不难!(附保姆级教程)
人工智能·spring boot·agent