package cn.report.excel.easyexcel.custom;

import java.util.List;
import org.apache.poi.ss.usermodel.IndexedColors;
import cn.report.excel.easyexcel.api.metadata.Head;
import cn.report.excel.easyexcel.api.write.metadata.style.WriteCellStyle;
import cn.report.excel.easyexcel.api.write.metadata.style.WriteFont;
import cn.report.excel.easyexcel.api.write.style.AbstractVerticalCellStyleStrategy;

/**
 * 单独设置表头某些单元格的样式
 * 例如：单独设置第1和3个表头单元为红色（从0开始）
 * List<Integer> columnIndexes = Arrays.asList(1,3);
   CustomCellStyleStrategy customCellStyleStrategy = new CustomCellStyleStrategy(columnIndexes);
   .registerWriteHandler(customCellStyleStrategy)
 * */
public class SamCellStyleStrategy extends AbstractVerticalCellStyleStrategy {

    /**
     * 操作列
     */
    private List<Integer> columnIndexes;

    public SamCellStyleStrategy( List<Integer> columnIndexes ) {
        this.columnIndexes = columnIndexes;
    }

    @Override
    protected WriteCellStyle headCellStyle(Head head) {

        // 获取样式实例
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 获取字体实例
        WriteFont headWriteFont = new WriteFont();
        // 设置字体样式
        headWriteFont.setFontName("宋体");
        // 设置字体大小
        headWriteFont.setFontHeightInPoints((short)14);
        // 边框
        headWriteFont.setBold(true);
        // 设置表头单元格必填时为红色
        if (columnIndexes.contains(head.getColumnIndex())){
            headWriteCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
        }
		//非必填时为蓝色
		else{
            headWriteCellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
        }
        headWriteCellStyle.setWriteFont(headWriteFont);
        return headWriteCellStyle;
    }
}
