freemarker学习+集成springboot+导出word

目录

[一 FreeMarker简介](#一 FreeMarker简介)

[二 集成springboot,实现案例导出](#二 集成springboot,实现案例导出)

[三 常见面试题总结](#三 常见面试题总结)


一 FreeMarker简介

FreeMarker 是一款 模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页,电子邮件,配置文件,源代码等)的通用工具。 是一个Java类库。

二 集成springboot,实现案例导出

在本地磁盘随便准备一个文件,内容体如下:

内容案例如下:

代码实现:

2.1 导入jar

复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

2.2 新建FileController.java类,代码实现如下:

java 复制代码
package com.yty.system.controller;


import com.alibaba.fastjson.JSONObject;
import freemarker.template.Configuration;
import freemarker.template.Template;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

@RestController
@RequestMapping("/file")
@Api(tags = "文件管理api")
public class FileController {


    @GetMapping("exportPDF")
    @ApiOperation(value = "文件导出到PDF",notes = "文件导出到PDF")
    public void exportPDF(HttpServletResponse response) throws Exception{
        try {
            exportWord(response, "2012001.ftl", "/templates/ftl/");
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void exportWord(HttpServletResponse response, String templateName, String templatePath) throws Exception{
        Configuration configuration = new Configuration(Configuration.getVersion()); // 创建一个Configuration对象

        configuration.setClassForTemplateLoading(this.getClass(), templatePath);

        configuration.setDefaultEncoding("utf-8");

        //必须加此参数,否则任意key的值为空freemark都会报错
        configuration.setClassicCompatible(true);
        // 选择模板
        Template template = configuration.getTemplate(templateName); //加载模板

        // 导出文件名
        String fileName = System.currentTimeMillis() + ".doc";
        // 导出文件路径
        String path = "D:\\system\\ee\\模板\\" + fileName;
        // 创建文件
        File file = new File(path);

        Writer out = new FileWriter(file);
        // 填充数据
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("surveyPersonName", "孙悟空");
        jsonObject.put("createTime", "2012-09");

        //调用模板对象的process方法输出文件
        template.process(jsonObject, out);

        // 下载文件
        downloadFile(response, file, out);
    }

    public void downloadFile(HttpServletResponse response, File file, Writer out) throws Exception{
        // 下载文件
        byte[] buffer = new byte[1024];
        response.addHeader("Content-Disposition","attachment;filename=" + new String(file.getName().getBytes(), "ISO-8859-1"));
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
        OutputStream os = response.getOutputStream();
        int i = bis.read(buffer);
        while (i != -1) {
            os.write(buffer, 0, i);
            i = bis.read(buffer);
        }
        // 关闭流
        os.close();
        bis.close();
        fis.close();
        out.close();
        file.delete();
    }
}

直接复制代码,运行结果:

集成完毕,数据已填充,导出完毕

三 常见面试题总结

待补充...............

相关推荐
心祭丫丫3 小时前
【每天一个 Python 小案例17】Python 实现通讯录
开发语言·人工智能·笔记·python·学习
露天赏雪3 小时前
第3篇:Sentinel熔断降级实战:大模型接口抖动时的服务兜底方案
java·开发语言·人工智能·spring boot·spring·servlet·sentinel
Nerd(BUG版)6 小时前
Flutter学习day5--事件手势与命中测试
学习·flutter
IHaveADiscovery6 小时前
无痛让AI由Markdown转Word:Pandoc模板了解一下
人工智能·word·markdown
Nerd(BUG版)7 小时前
KMP学习day4--Kotlin空安全与泛型
学习·安全·kotlin
東隅已逝,桑榆非晚7 小时前
算法6. 双指针法-查找总价格为目标的两个商品
c++·学习·算法
wdyan2978 小时前
带着小白学习-微服务-5 实战:配置中心-数据库连接
数据库·学习·微服务
叼烟扛炮9 小时前
Linux 学习笔记(五十):网络层——IP 协议、子网划分与 NAT 魔法
linux·笔记·学习
叼烟扛炮9 小时前
Linux 学习笔记(六十):单线程的逆袭——epoll 高并发服务器实战
linux·服务器·网络·笔记·学习·算法
蜗牛87510 小时前
为 Spring Boot 团队做一个 Elasticsearch 查询工作台:六种模式、语义补全与浏览器直连
spring boot