freemark导出word

8 篇文章 0 订阅
4 篇文章 0 订阅

freemark填充表格

pom.xml 添加jar

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

先制作word模板

在这里插入图片描述

另存为xml格式

在这里插入图片描述

ide 打开 添加循环标签

 <#list listInfo as list>
 循环
  </#list>

代码

    public  void createWord(Map dataMap, String templateName, String filePath) {
        try {
            //创建配置实例
            Configuration configuration = new Configuration();
            //设置编码
            configuration.setDefaultEncoding("UTF-8");
            //ftl模板文件
            //configuration.setClassForTemplateLoading(Student.class,"/");
            //获取模板
           // Template template = configuration.getTemplate("freemark.ftl");

            configuration.setDirectoryForTemplateLoading(new File("C:\\Users\\pact.PACT-20190329SP\\Desktop")); // FTL文件所存在的位置
               // configuration.setClassForTemplateLoading(this.getClass(), ftlPath);//以类加载的方式查找模版文件路径
            Template template = configuration.getTemplate("freemark.ftl");

            //输出文件
            File outFile = new File(filePath);
            //如果输出目标文件夹不存在,则创建
            if (!outFile.getParentFile().exists()) {
                outFile.getParentFile().mkdirs();
            }
            //将模板和数据模型合并生成文件
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
            //生成文件
            template.process(dataMap, out);

            //关闭流
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值