package com.muchinfo.mtp3common.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.muchinfo.mtp3common.vo.ExportResult;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Base64;
import java.util.Collection;

public class ExportUtil {

    public static ExportResult write( String fileName, Class<?> clazz, Collection<?> data) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        EasyExcel.write(byteArrayOutputStream)
                .head(clazz)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet(fileName)
                .doWrite(data);
        byte[] bytes = byteArrayOutputStream.toByteArray();
        return ExportResult.builder().fileName(fileName).base64(Base64.getEncoder().encodeToString(bytes)).build();
    }

}
