花花_新世界
从【程序猿】到【程序员】的进化历程

背景:政府项目很多用到审核完记录后填充到word文档中导出打印的功能

话不多说上代码


public void exportDetectContractFiling(){
QingDaMap qingDaMap = QingDaMap.NEW()
                .setv("xxx", "测试内容"));
        FreeMarkerTemplateUtil.fillTemplateMap("xxxx登记表.docx", QualityExportTemplateEnum.ENTRUSTMENT_REGISTRATION_FORM,
                qingDaMap);
}
package com.ruoyi.common.utils;


import freemarker.cache.ClassTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URLEncoder;

/**
 * 自由标记模板跑龙套
 *
 * @author doubleflower
 * @date 2023/08/08
 */
public class FreeMarkerTemplateUtil {
    private static final Configuration CONFIGURATION;

    private FreeMarkerTemplateUtil() {
    }

    public static Template getTemplate(String templateName) throws IOException {
        try {
            return CONFIGURATION.getTemplate(templateName);
        } catch (IOException var2) {
            throw var2;
        }
    }

    public static void fillTemplateEntity(String fileName, BaseTemplateExport templateName, Object data) {
        fillTemplateMap(fileName, templateName, QingDaMap.wrap(QingDaBeanUtil.javaBeanToMap(data)));
    }

    public static void fillTemplateMap(String fileName, BaseTemplateExport templateName, QingDaMap data) {
        try {
            Template template = getTemplate(templateName.getTemplateName());
            HttpServletResponse response =
                    ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
            response.reset();
            response.setContentType("application/octet-stream");
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            PrintWriter out = response.getWriter();
            template.process(data, out);
            out.close();
        } catch (Throwable var6) {
            throw new RuntimeException(var6);
        }
    }


    public static InputStream fillStreamTemplateMap(BaseTemplateExport templateName, QingDaMap data) {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            OutputStreamWriter writer = new OutputStreamWriter(byteArrayOutputStream);
            Template template = getTemplate(templateName.getTemplateName());
            template.process(data, writer);
            writer.flush();
            return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        } catch (Throwable var6) {
            throw new RuntimeException(var6);
        }
    }

    public static void clearCache() {
        CONFIGURATION.clearTemplateCache();
    }

    static {
        CONFIGURATION = new Configuration(Configuration.VERSION_2_3_31);
        CONFIGURATION.setTemplateLoader(new ClassTemplateLoader(FreeMarkerTemplateUtil.class, "/templates"));
        CONFIGURATION.setDefaultEncoding("UTF-8");
        CONFIGURATION.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    }
}
package com.ruoyi.common.enums;

import com.ruoyi.common.utils.BaseTemplateExport;
import lombok.AllArgsConstructor;

/**
 * 质量检测模板枚举
 *
 * @author doubleflower
 * @date 2023/08/01
 */
@AllArgsConstructor
public enum QualityExportTemplateEnum implements BaseTemplateExport {

   

    /**
     * 建设工程现场检测方案登记表
     */
    ENTRUSTMENT_REGISTRATION_FORM {
        /**
         * 模板路径
         *
         * @return {@link String}
         */
        @Override
        public String getTemplateName() {
            return "detectContractFiling.ftl";
        }
    }
    

}

posted on 2024-11-07 17:03  花花_新世界  阅读(47)  评论(0编辑  收藏  举报  来源