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);
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章