package com.zcxk.common;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.alibaba.excel.support.ExcelTypeEnum;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;

/**
 * @ClassName EasyExcelUtil
 * @Description: Excel导出通用工具类
 * @Author :WYY
 * @Date 2020/10/27
 * @Version V1.0
 **/
public  class EasyExcelUtil {
    /**
     * @MethodName:Excel导出
     * @Description: TODO
     * @Param: path 导出基础路径 t实体名 excelName 文件名 data数据
     * @Return:
     * @Author: WYY
     * @Date: 2020/10/28
     **/
    public static String excelWrite(String path,Class t, String excelName,List data) {
        String filePath = "";
        String fileName = DateFormatUtils.format(new Date(), "yyyy/MM/dd") + File.separator;
        fileName = fileName + excelName +"-"+ DateFormatUtils.format(new Date(), "yyyyMMddHHmmss")  + ".xlsx";
        try {
            getAbsoluteFile(path, fileName);
            EasyExcel.write(path + fileName, t)
                    .excelType(ExcelTypeEnum.XLSX)
                    .registerWriteHandler(new CustemhandlerUtils())
                    .registerWriteHandler(EasyExcelUtil.getStyleStrategy())
                    .sheet("Export")
                    .doWrite(data);
            filePath = getPathFileName(path, fileName);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
        return filePath;
    }
    private static final String getPathFileName(String uploadDir, String fileName) throws IOException {
        String pathFileName = uploadDir  + "/" + fileName;
        return ToolUtil.path(pathFileName);
    }
    private static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException {
        File desc = new File(uploadDir + File.separator + fileName);

        if (!desc.getParentFile().exists()) {
            desc.getParentFile().mkdirs();
        }

        return desc;
    }
    public static HorizontalCellStyleStrategy getStyleStrategy(){
        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 背景设置为灰色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short)12);
        // 字体样式
        headWriteFont.setFontName("Frozen");
        headWriteCellStyle.setWriteFont(headWriteFont);
        //自动换行
        headWriteCellStyle.setWrapped(false);
        // 水平对齐方式
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        // 垂直对齐方式
        headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
//        contentWriteCellStyle.setFillPatternType(FillPatternType.SQUARES);
        // 背景白色
        contentWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        WriteFont contentWriteFont = new WriteFont();
        // 字体大小
        contentWriteFont.setFontHeightInPoints((short)12);
        // 字体样式
        contentWriteFont.setFontName("Calibri");
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    }
}