package com.huiju.estateagents.common;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.huiju.estateagents.excel.ConsultantKPIExport;
import com.huiju.estateagents.excel.handler.CustomCellWriteHandler;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

public class ExcelUtils {

    /**
     * 发送 excel 到客户端
     * 暂时只支持单 sheet 页
     * @param response
     * @param data
     * @param fileName
     * @throws IOException
     */
    public static void flush(HttpServletResponse response, Class dataClass, List data, String fileName) throws IOException {
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename="+StringUtils.strToUnicode(fileName)+".xlsx");

//        ParameterizedType type = (ParameterizedType) data.getClass().getGenericSuperclass();
//        Class dataClass = (Class) type.getActualTypeArguments()[0];

        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), dataClass).registerWriteHandler(new CustomCellWriteHandler()).build();
        WriteSheet sheet1 = EasyExcel.writerSheet("sheet1").build();
        excelWriter.write(data, sheet1);
        excelWriter.finish();
    }
}
