package com.izouma.jiashanxia.utils.excel;

import com.alibaba.excel.EasyExcel;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class ExcelUtils<T> {
    public static <T> void export(HttpServletResponse response, List<T> data) throws IOException {
        String fileName = "data.xlsx";
        if (data != null && !data.isEmpty()) {
            fileName = data.get(0).getClass().getSimpleName() + ".xlsx";
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        EasyExcel.write(response.getOutputStream(), data.get(0).getClass()).sheet("sheet")
                 .registerConverter(new LocalDateConverter())
                 .registerConverter(new LocalDateTimeConverter())
                 .doWrite(data);
    }
}
