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();
    }
}

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

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

三 常见面试题总结

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

相关推荐
Katzelala3 小时前
[K8S学习笔记] Service和Ingress的关系
笔记·学习·kubernetes
有谁看见我的剑了?3 小时前
k8s-init容器学习
学习·容器·kubernetes
HAH-HAH4 小时前
【Python 入门】(2)Python 语言基础(变量)
开发语言·python·学习·青少年编程·个人开发·变量·python 语法
小沈同学呀5 小时前
创建一个Spring Boot Starter风格的Basic认证SDK
java·spring boot·后端
xian_wwq6 小时前
【学习笔记】Https证书如何升级到国密
笔记·学习·证书
凤山老林8 小时前
SpringBoot 轻量级一站式日志可视化与JVM监控
jvm·spring boot·后端
Chan168 小时前
JVM从入门到实战:从字节码组成、类生命周期到双亲委派及打破双亲委派机制
java·jvm·spring boot·后端·intellij-idea
The_Killer.9 小时前
格密码--从FFT到NTT(附源码)
学习·线性代数·密码学·格密码
四谎真好看10 小时前
Java 黑马程序员学习笔记(进阶篇6)
java·笔记·学习·学习笔记