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}
相关推荐
尘觉3 小时前
中秋节与 Spring Boot 的思考:一场开箱即用的团圆盛宴
java·spring boot·后端
又是忙碌的一天4 小时前
前端学习day01
前端·学习·html
不会算法的小灰5 小时前
HTML简单入门—— 基础标签与路径解析
前端·算法·html
cooldream20096 小时前
深度解析中秋节HTML5动画的实现
前端·html·html5
半旧夜夏6 小时前
【设计模式】核心设计模式实战
java·spring boot·设计模式
皮皮林5516 小时前
SpringBoot 控制台秒变炫彩特效,秀翻同事指南!
spring boot
IT学长编程7 小时前
计算机毕设选题 基于SpringBoot的书店管理系统的设计与实现 网上书店系统 前后端分离 Java毕设项目 毕业设计选题 【附源码+文档报告+安装调试】
java·spring boot·毕业设计·课程设计·前后端分离·网上书店系统·书店管理系统
IT学长编程8 小时前
计算机毕设选题 基于SpringBoot的房产租赁管理系统 房屋租赁系统 前后端分离 Java毕设项目 毕业设计选题 【附源码+文档报告+安装调试】
java·spring boot·毕业设计·课程设计·房屋租赁系统·房产租赁系统·文档报告
李慕婉学姐8 小时前
【开题答辩过程】以《基于 Spring Boot 的宠物应急救援系统设计与实现》为例,不会开题答辩的可以进来看看
数据库·spring boot·宠物