java-Freemark实现word 、excel 模板导出

Freemark实现word 、excel 模板导出

1.引入依赖

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

2.将wrod或者excel 另存为xml文件,然后在xml文件中插入freemark语法,最后将xml文件.xml后缀修改为.ftl(具体操作百度freemarker 生成ftl)

3.导出模板代码工具类

package com.coe.wms.util;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;
import lombok.Data;
import lombok.Getter;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * freemark 工具类
 *
 * @author aiyuan
 * @date 2019/11/22 14:03
 * @desc
 */
public   class FreemarkerUtil {
    private static final String encoding = "utf-8";
    private static final String version = "2.3.0";


    public static class TemplateHandler {
        private Template template;
        public TemplateHandler(String templatePath) throws Exception {
            // 模板文件
            File file = new File(templatePath);
            String templateName = file.getName();

            Configuration configuration = new Configuration(new Version(version));
            configuration.setDefaultEncoding(encoding);
            configuration.setDirectoryForTemplateLoading(new File(file.getParent()));

            //以utf-8的编码读取ftl文件
            Template template = configuration.getTemplate(templateName, encoding);
            this.template = template;
        }
        public void process(Object dataModel, String targetPath) throws Exception {
            // 文件保存地址
            File outFile = new File(targetPath);
            Writer out = null;
            try {
                out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), encoding), 10240);
                template.process(dataModel, out);
            } catch (Exception ee) {
                ee.printStackTrace();
                throw  ee;
            } finally {
                close(out);
            }
        }
        public void close(Writer out) {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

4.调用代码

测试类,类中的字段对应ftl文件中使用Freemarker语法的字段,orderItemList 集合字段也是

@Data
public class ImportLicenceDTO implements Serializable {

    /**
     * 出口公司名称
     */
    private  String exporterCompanyName;
    /**
     * 出口公司地址
     */
    private  String exporterCompanyAddress;

    /**
     * 订单明细
     */
    private List<ImportLicenceOrderItemDTO> orderItemList = new ArrayList<>();



    @Data
    public static class ImportLicenceOrderItemDTO implements Serializable {

        /**
         * 單位數量(如公斤,公升)
         */
        private  String specifications;
        /**
         * 到岸价
         */
        private  String cifPrice;


    }

}

实际调用

   public static void main(String[] args) throws Exception {
        // 生成测试数据
        // 明细集合
        ImportLicenceDTO.ImportLicenceOrderItemDTO itemDTO1 = new ImportLicenceDTO.ImportLicenceOrderItemDTO();
        itemDTO1.setCifPrice("100");
        itemDTO1.setSpecifications("10");
        ImportLicenceDTO.ImportLicenceOrderItemDTO itemDTO2 = new ImportLicenceDTO.ImportLicenceOrderItemDTO();
        itemDTO2.setCifPrice("100");
        itemDTO2.setSpecifications("10");

        ImportLicenceDTO dto = new ImportLicenceDTO();
        dto.setExporterCompanyAddress("上海");
        dto.setExporterCompanyName("测试");
        dto.setOrderItemList(Arrays.asList(itemDTO1,itemDTO2));
        ImportLicenceDTO dto2 = new ImportLicenceDTO();
        dto2.setExporterCompanyAddress("上海");
        dto2.setExporterCompanyName("测试");
        dto2.setOrderItemList(Arrays.asList(itemDTO1,itemDTO2));

        List<ImportLicenceDTO> list = Arrays.asList(dto, dto2);
        TemplateHandler handler = new TemplateHandler("C:\\Users\\-AiYuan\\Desktop\\进口证.ftl");

        // 批量导出 每个对象生成不同的doc文件
        for (ImportLicenceDTO item : list) {
            final String filePathAndName = "C:\\Users\\-AiYuan\\Desktop\\" + "-" + "进口证" + "-" + System.currentTimeMillis() + ".doc";
            handler.process(item, filePathAndName);
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值