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

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

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

三 常见面试题总结

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

相关推荐
shepherd12610 小时前
破局延时任务(下):Spring Boot + DelayQueue 优雅实现分布式延时队列(实战篇)
java·spring boot·分布式
程序员零一11 小时前
Spring Boot 多 RabbitMQ 连接集成指南
spring boot·rabbitmq
tt55555555555511 小时前
《神经网络与深度学习》学习笔记一
深度学习·神经网络·学习
安冬的码畜日常11 小时前
【JUnit实战3_28】第十七章:用 JUnit 5 实测 SpringBoot 项目
spring boot·功能测试·测试工具·设计模式·单元测试·junit5
李慕婉学姐11 小时前
Springboot的民宿管理系统的设计与实现29rhm9uh(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端
李慕婉学姐11 小时前
【开题答辩过程】以《基于微信小程序垃圾分类图像识别技术实现》为例,不会开题答辩的可以进来看看
spring boot·微信小程序·vue
Kay_Liang12 小时前
Spring中@Controller与@RestController核心解析
java·开发语言·spring boot·后端·spring·mvc·注解
TL滕12 小时前
从0开始学算法——第一天(如何高效学习算法)
数据结构·笔记·学习·算法
陈果然DeepVersion12 小时前
Java大厂面试真题:Spring Boot+Kafka+AI智能客服场景全流程解析(七)
java·人工智能·spring boot·微服务·kafka·面试题·rag
仰望—星空12 小时前
MiniEngine学习笔记 : CommandAllocatorPool
笔记·学习