使用spring boot freemarker导出word模板


最近在项目中遇到一个功能,从数据库中获取数据,然后将这些数据写入word模板中,网上也有很多资料和样例,主要步骤大概就是创建一个word文件,word文件中需要数据库保存的地方用占位符来写:${}
在这里插入图片描述

文件创建好之后先另存为其他格式,选择(.xml)格式
将另存为的,xml格式文件复制到项目目录下的resource/template目录下,这时候将文件的格式修改为ftl格式。
在这里插入图片描述
到这里开始工作差不多已经完成,下面主要编写业务代码:

@RequestMapping("/

## enterprise

/getDoc/{id}")
public void getDoc(@PathVariable Long id,
                       HttpServletRequest request, HttpServletResponse response) throws Exception {
        RegisterEnterpriseDTO registerEnterpriseDTO = registerEnterpriseService.getDetail(id);
        Map<String,String> dataMap = new HashMap<String,String>();
        dataMap.put("name", "拟申请名称")
        dataMap.put("enterpriseProperty", "企业性质");
        dataMap.put("industry","行业类别";
        dataMap.put("regAddress", "注册地址");
        dataMap.put("address", "经营地址");
        dataMap.put("regCapital","注册资本");
        dataMap.put("exportShareholder", "股东信息");
        dataMap.put("legalName", "法人代表");
        dataMap.put("legalCard", "法人代表证件号");
        dataMap.put("legalTel", "联系电话");
        dataMap.put("contact", "联系人名称");
        dataMap.put("contactIdentityCard", "联系人证件号");
        dataMap.put("contactTel", "联系电话");
        dataMap.put("businessScope", "经营范围";
		//设置表明
        String newWordName = "企业入驻信息采集表.doc";
        //调用打印word的函数
        DocUtil.download(request, response, newWordName, dataMap);
    }

导出帮助类

import freemarker.cache.FileTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Map;

public class DocUtil {

    public static void download(HttpServletRequest request, HttpServletResponse response, String newWordName, Map dataMap) throws Exception{
        Configuration configuration = new Configuration(Configuration.getVersion());
        configuration.setDefaultEncoding("utf-8");
        Template t = null;
   		//加载模板所在的目录,这里面遇到很坑的问题
        configuration.setClassForTemplateLoading(configuration.getClass(), "/template");
        try {
            //word.xml是要生成Word文件的模板文件
            t = configuration.getTemplate("word.ftl","utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        File outFile = null;
        Writer out = null;
        String filename = newWordName;
        try {
            outFile = new File(newWordName);
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(outFile),"utf-8"));

        } catch (Exception e1) {
            e1.printStackTrace();
        }

        try {
            t.process(dataMap, out);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        InputStream fis = null;
        OutputStream toClient = null;
        try {
            fis = new BufferedInputStream(new FileInputStream(outFile));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            // 设置response的Header
            filename = URLEncoder.encode(filename, "utf-8");
            response.addHeader("Content-Disposition", "attachment;filename=" + filename+"");
            response.addHeader("Content-Length", "" + outFile.length());
            toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            try {
                if(fis!=null){
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(toClient!=null){
                    toClient.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

到这里导出功能基本上就完成,但是在时间开发中遇到了各种坑:

遇到坑一: 导出的word里面像是 .xml文件信息,空置太保存,显示有某个字段出错

经检测最后确定 在word的模板中所有的占位符都需要有对用的值,不能是null,如果在业务中获取的某个字段是null,需要手动去赋予空字符串,就是不能是null,占位符要和map里面的数据对应上,map里面的数据不能比模板里面的占位符少

遇到坑二:加载不到模板

Freemarker提供了3种加载模板目录的方法。 它使用Configuration类加载模板

3种方法分别是:
一、基于类路径:
public void setClassForTemplateLoading(Class clazz, String pathPrefix);

Configuration cfg = new Configuration();
cfg.setClassForTemplateLoading(cfg.class, "/template");
cfg.getTemplate("word.ftl");

这里注意一下第二个参数需要以 “/” 开头。

二、文件系统:
public void setDirectoryForTemplateLoading(File dir) throws IOException;

Configuration cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(new File("/home/user/template"));
cfg.getTemplate("word.ftl");

三、Servlet Context:
public void setServletContextForTemplateLoading(Object servletContext, String path);
看名字也就知道了,分别基于类路径、文件系统以及Servlet Context。

我开始使用的是第二种,但是在使用idea启用的时候完全没有问题,但是在项目部署的时候获取不到路径,我是用spring boor 生成的jar,无法获取,使用绝对路径更是不行,在new File的时候出错,提示模板路径不存在。后来使用第一种方式,用类加载的方式,然后使用的是相对路径。部署之后成功,在idea中也没有问题。

导出帮助类也是在网上找的,里面部分代码在我在不适用,稍作修改

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
SpringBoot_Freemarker生成Word_多个表格+两层嵌套循环; 步骤说明: 1.用Microsoft Office Word打开word原件;将文档中需要动态生成的内容,替换为属性名 ${name} 2.另存为,选择保存类型Word 2003 XML 文档(*.xml) 3.用Firstobject free XML editor打开文件,选择Tools下的Indent【或者按快捷键F8】格式化文件内容。左边是文档结构,右边是文档内容; 4. 文档生成后有时需要手动修改,查找第一步中设置的属性名,可能会产生类似${n.....ame}类似的样子,我们将将名字中间的标签删掉,恢复为${name} 5. word模板中有表格,需要循环的位置, 用 标签将第二对 标签(即除表头的w:tr标签后的一对)包围起来 同时表格内的属性例如${name},在这里需要修改为${user.name} (userList是集合在dataMap中的key, user是集合中的每个元素, 类似), 如图: PLUS:若表格之外还有嵌套的循环,也需要用,注意这里的标签不要和某对其他标签交叉,不可以出现这种 6. 标识替换完之后,另存为.ftl后缀文件即可。 代码里是相对有一丢丢复杂的,两层嵌套循环; 总(dataMap) deptName 部门名 list(Table)表的集合 table1(map) table-名字 ${map.table} tableName-中文名 ${map.tableName} columnCount-字段数 ${map.columnCount} recordCount-记录数 ${map.recordCount} listA-List--表格1 map.listA column Model属性——字段名 ${model.column} columnName Model属性——字段中文名 ${model.column} rate Model属性——字段占比 ${model.rate} nullValueCount Model属性——字段空值数 ${model.nullValueCount} listB-List--表格2 map.listB …… listC-List--表格3 map.listC …… table2 table-名字 ${map.table} tableName-中文名 ${map.tableName} columnCount-字段数 ${map.columnCount} recordCount-记录数 ${map.recordCount} listA-List--表格1 map.listA column Model属性——字段名 ${model.column} columnName Model属性——字段中文名 ${model.column} rate Model属性——字段占比 ${model.rate} nullValueCount Model属性——字段空值数 ${model.nullValueCount} listB-List--表格2 map.listB …… listC-List--表格3 map.listC …… table3 ……
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一行特立独行的代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值