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}
相关推荐
昵称为空C5 分钟前
SpringBoot3 http接口调用新方式RestClient + @HttpExchange像使用Feign一样调用
spring boot·后端
前端老鹰3 小时前
HTML <output> 标签:原生表单结果展示容器,自动关联输入值
前端·html
玩泥巴的4 小时前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
word·二次开发·office·com互操作
芦苇Z6 小时前
HTML <a> 标签的 rel 属性全解析:安全、隐私与 SEO 最佳实践
前端·html
麦兜*9 小时前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud
麦兜*9 小时前
MongoDB 在物联网(IoT)中的应用:海量时序数据处理方案
java·数据库·spring boot·物联网·mongodb·spring
汤姆yu9 小时前
基于springboot的毕业旅游一站式定制系统
spring boot·后端·旅游
Alice-YUE9 小时前
【CSS学习笔记3】css特性
前端·css·笔记·html
计算机毕业设计木哥10 小时前
计算机毕设选题推荐:基于Java+SpringBoot物品租赁管理系统【源码+文档+调试】
java·vue.js·spring boot·mysql·spark·毕业设计·课程设计
hdsoft_huge20 小时前
Java & Spring Boot常见异常全解析:原因、危害、处理与防范
java·开发语言·spring boot