栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Java

java使用ftl模板导出word文档

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java使用ftl模板导出word文档

一、渲染普通数据 1、创建word模板

2、另存为XML格式文件

3、创建java程序

4、将xml文件改名为ftl后缀放在resources下

5、格式化代码

6、使用${}替换原有的数据

7、添加依赖
        
            org.freemarker
            freemarker
            2.3.31
        
8、工具类
package com.ftl.test.utils;


import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
import lombok.extern.log4j.Log4j2;

import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;

import java.io.*;
import java.util.*;


@Component
@Log4j2
public class FtlUtils {

   private static final String ENCODING = "UTF-8";
   private static Configuration cfg = new Configuration();


   
   public static void reportPeisOrgReservation(Map data,HttpServletResponse response) {
       try {
           reportPeisOrgReservation(data, "ftl模板.ftl", "logs/" + "aaa" + ".docx", "aaa" + ".docx", response);
       } catch (Exception e) {
           e.printStackTrace();
       }
   }
   

   // 初始化cfg
   static {
       // 设置模板所在文件夹
       cfg.setClassForTemplateLoading(FtlUtils.class, "/templates/word");
       // setEncoding这个方法一定要设置国家及其编码,不然在ftl中的中文在生成html后会变成乱码
       cfg.setEncoding(Locale.getDefault(), ENCODING);
       // 设置对象的包装器
       cfg.setObjectWrapper(new DefaultObjectWrapper());
       // 设置异常处理器,这样的话就可以${a.b.c.d}即使没有属性也不会出错
       cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);

   }

   // 获取模板对象
   public static Template getTemplate(String templateFileName) throws IOException {
       return cfg.getTemplate(templateFileName, ENCODING);
   }
   
   
   public static void reportPeisOrgReservation(Map data, String templateFileName, String outFilePath, String fileName, HttpServletResponse response) throws Exception {
       Writer out = null;
       File outFile = new File(outFilePath);
       try {
           // 获取模板,并设置编码方式,这个编码必须要与页面中的编码格式一致
           Template template = getTemplate(templateFileName);
           if (!outFile.getParentFile().exists()) {
               outFile.getParentFile().mkdirs();
           }
           out = new OutputStreamWriter(new FileOutputStream(outFile), ENCODING);
           //下载到本地
           template.process(data, out);
           out.flush();
           log.info("由模板文件" + templateFileName + "生成" + outFilePath + "成功.");
       } catch (Exception e) {
           log.error("由模板文件" + templateFileName + "生成" + outFilePath + "出错");
       } finally {
           out.close();
       }
       // 获取服务器本地的文件位置
       File file = new File(outFilePath);
       if (file.exists()) {
           BufferedInputStream bufferedInputStream = null;
           BufferedOutputStream bufferedOutputStream = null;
           try {
               // 清除buffer缓存
               response.reset();
               // 指定下载的文件名
               response.setHeader("Content-Disposition",
                       "attachment;filename=" + fileName);
               response.setContentType("application/vnd.ms-excel;charset=UTF-8");
               response.setHeader("Pragma", "no-cache");
               response.setHeader("Cache-Control", "no-cache");
               FileInputStream inputStream = new FileInputStream(file);
               bufferedInputStream = new BufferedInputStream(inputStream); //缓冲流加速读
               OutputStream outputStream = response.getOutputStream();
               bufferedOutputStream = new BufferedOutputStream(outputStream);  //缓冲流加速写
               int n;
               while ((n = bufferedInputStream.read()) != -1) {
                   bufferedOutputStream.write(n);
               }
           } catch (Exception e) {
               e.printStackTrace();
           } finally {
               try {
                   bufferedOutputStream.close();
                   bufferedInputStream.close();
                   file.delete();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
       } else {
           throw new RuntimeException("文件在本地服务器不存在");
       }
   }
}
9、装入假数据

pojo类

@Data
@AllArgsConstructor
public class Person {

    private String name;

    private Integer age;

    private Integer height;

    private Integer weight;
}

controller

@RestController
public class FtlController {

    @GetMapping("/testFtl")
    public void testFtl(HttpServletResponse response){
        // 此条数据是通过数据库查询出的数据
        Person person = new Person("李四",13,173,56);
        Map map = new HashMap<>();
        map.put("person",person);
        FtlUtils.reportPeisOrgReservation(map,response);
    }
}
二、列表渲染 1、先仿假数据
@RestController
public class FtlController {

    @GetMapping("/testFtl")
    public void testFtl(HttpServletResponse response){
        // 此条数据是通过数据库查询出的数据
        Person person = new Person("李四",13,173,56);
        Map map = new HashMap<>();

        // 此数据是查询数据库返回的集合
        List personList = new ArrayList<>();
        personList.add(new Person("李四1",13,173,53));
        personList.add(new Person("李四2",14,174,54));
        personList.add(new Person("李四3",15,175,55));
        personList.add(new Person("李四4",16,176,56));
        personList.add(new Person("李四5",17,177,57));

        // 添加到map传给模板
        map.put("person",person);
        map.put("personList",personList);
        FtlUtils.reportPeisOrgReservation(map,response);
    }
}
2、这个就是那个列表

3、使用遍历包围父标签

4、渲染数据

5、访问controller自动下载(演示)

三、忘了,我是傻逼,如果列表前面有id编号的话如1.2.3.4

和渲染列表中一样,将编号哪一行的数据改为

${person?if_exists+1}
转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1040745.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号