java使用ftl模板导出word文档

一、渲染普通数据

1、创建word模板

在这里插入图片描述

2、另存为XML格式文件

在这里插入图片描述

3、创建java程序

在这里插入图片描述

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

在这里插入图片描述

5、格式化代码

在这里插入图片描述

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

在这里插入图片描述

7、添加依赖

        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.31</version>
        </dependency>

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.*;

/**
* @description:
* @author: 马大厨
* @DAte:2022/8/9
*/
@Component
@Log4j2
public class FtlUtils {

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


   /**
    * 核心方法
    * @param response 返回对象
    * @param data 渲染装载数据
    */
   public static void reportPeisOrgReservation(Map<String, Object> 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);
   }
   
   /**
    * 先根据模板生成word在本地,然后再上传到file文档服务器,最后删除本地word
    *
    * @param data             Map的数据结果集
    * @param templateFileName ftl模版文件名
    * @param outFilePath      生成文件名称(可带路径)
    * @throws Exception
    */
   public static void reportPeisOrgReservation(Map<String, Object> 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<String,Object> 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<String,Object> map = new HashMap<>();

        // 此数据是查询数据库返回的集合
        List<Person> 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}
  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Java中的Freemarker模板导出Word文件时,默认情况下,文件的格式是由文件名后缀决定的。如果你想将其另存为doc格式而不是xml格式,你可以尝试以下方法: 1. 确保文件名的后缀为.doc或.docx。在保存文件时,将文件名设置为以.doc或.docx结尾,如"example.doc"或"example.docx"。 2. 确保使用的是正确的输出流。在保存文件时,确保你使用正确的输出流类型来保存Word文件。例如,使用FileOutputStream来保存文件。 以下是一个简单的示例代码,展示了如何使用Freemarker模板导出Word文件并将其另存为doc格式: ```java import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; import java.io.*; public class WordExport { public static void main(String[] args) { Configuration configuration = new Configuration(Configuration.VERSION_2_3_31); configuration.setDefaultEncoding("UTF-8"); configuration.setClassForTemplateLoading(WordExport.class, "/templates"); try { Template template = configuration.getTemplate("template.ftl"); File outputFile = new File("output.doc"); Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8")); template.process(dataModel, writer); writer.close(); } catch (IOException | TemplateException e) { e.printStackTrace(); } } } ``` 在上述代码中,我们将文件名设置为"output.doc",这样保存的就是doc格式的Word文件。 请确保你在代码中适当地替换模板路径、数据模型和文件名,以适应你的具体情况。 希望这能解决你的问题!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值