freemarker导出excel(复杂表单导出)

导入依赖

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

工具类

package com.gdgxkj.rep.proposal.utils.export;

import freemarker.template.Configuration;
import freemarker.template.Template;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URLEncoder;
import java.util.Map;

/**
 *@BelongsPackage: com.gdgxkj.rep.proposal.utils.export
 *@Author: chenlf
 *@CreateTime: 2023-03-21
 *@Description: FreeMarkerUtil
 *@Version: 1.0
 */
public class FreeMarkerUtil {

    /**
     * @title: ExportExcel
     * @description: 导出excel
     * @author: chenlf
     * @date: 2023/4/24
     * @param: fileName 
     * @param: templateName
     * @param: data
     * @return: void
     */
    public static void ExportExcel(String fileName, String templateName, Map<String,Object> data) throws IOException {
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        response.reset();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");
        OutputStreamWriter writer = null;
        try {
            Template template = FreeMarkerUtil.getTemplate(templateName);
            writer = new OutputStreamWriter(response.getOutputStream(), "UTF-8");
            template.process(data, writer);
            writer.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }

    private static Template getTemplate(String templateName) throws IOException {
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_26);
        configuration.setDefaultEncoding("utf-8");
        configuration.setClassForTemplateLoading(FreeMarkerUtil.class,"/template/freemarker");
        return configuration.getTemplate(templateName);
    }

}

模板

将Excel文档另存为.xml文件
然后采用freemarker标签动态调整填充数据即可
在这里插入图片描述

调用

	Map<String,Object> data = new HashMap<>();
	data.put("XX", "test");
	data.put("results", resultList);
	try {
		FreeMarkerUtil.ExportExcel("统计", "rep-undertake-suggest.xml", data);
	} catch (Exception e) {
		e.printStackTrace();
	}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值