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

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

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

三 常见面试题总结

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

相关推荐
程序员zgh2 分钟前
C++ 环形队列 从原理到实例演示
c语言·开发语言·数据结构·c++·学习
observe1016 分钟前
ARM学习之时钟,EPIT,GPT
arm开发·学习
想进大厂的小徐7 分钟前
Spring 容器启动与 Bean 创建流程
java·spring boot·spring
婷婷_1728 分钟前
【PCIe验证每日学习·Day21】PCIe复位机制与功能级复位(FLR)全解析
学习·程序人生·芯片·pcie·芯片验证·链路恢复·pcie 复位
真上帝的左手13 分钟前
12. 消息队列-RabbitMQ-Spring Boot 集成 RabbitMQ
spring boot·rabbitmq·java-rabbitmq
observe10117 分钟前
ARM学习之中断,按键
学习
果果燕19 分钟前
ARM嵌入式学习(一)---ARM基础概念学习
arm开发·学习
码喽7号19 分钟前
Vue学习三:element-plus组件和FontAwesome图标组件
前端·vue.js·学习
zhangren0246821 分钟前
Laravel5.x版本特性全解析
android·vue.js·spring boot·mysql
Meepo_haha21 分钟前
ES在SpringBoot集成使用
spring boot·elasticsearch·jenkins