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}
相关推荐
她的男孩5 分钟前
企业接口照样拦得住:独立 Flyway、@RequiresFeature 与离线许可证
java·spring boot·后端
十年Java程序媛5 分钟前
Lambda 与函数式接口|别只会复制 ()->{},底层规则和坑一次性讲清
java·spring boot·后端
小凯在掘金1 小时前
Shadow DOM 和 Virtual DOM ?别再傻傻分不清
前端·html
马剑威(威哥爱编程)1 小时前
【AI全栈后端12-11】Spring Boot 把 AI 接口真正上线扛量:限流 / 降级 / 可观测
人工智能·spring boot·后端
RuoyiOffice2 小时前
SpringBoot+Vue3+Onlyoffice 企业文件生命周期:上传、业务关联、权限、版本与安全清理
spring boot·vue3·文件管理·对象存储·权限控制·ruoyi office·附件管理
阿狗童鞋2 小时前
高并发系统设计实战指南
java·spring boot·spring cloud
十年Java程序媛17 小时前
Java 接口和抽象类对比|Java8 新特性,抛弃老旧八股,正确选型
java·spring boot·后端
for_ever_love__19 小时前
MySQL 高频面试题 30 问:索引、事务、锁、日志、主从与分库分表(MySQL 8.0 版)
java·数据库·spring boot·mysql·spring cloud·面试·总结
SL_staff19 小时前
设备越多系统越崩?根源在三层抽象失效
spring boot·物联网·github
威哥爱编程19 小时前
【AI全栈12-01】Spring Boot 为何是 AI 全栈后端优选
java·spring boot·后端