package org.security.common.util;

import com.alibaba.excel.metadata.data.DataFormatData;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;

/**
 * @author Create by wzf
 * @date 2022/5/30 14:37
 *
 */
public class MyCellStyleStrategyUtil {
	/**
	 *
	 * @return
	 */
	public static HorizontalCellStyleStrategy getHorizontalCellStyleStrategy(){
		// 表头策略
		WriteCellStyle headerCellStyle = new WriteCellStyle();
		// 表头水平对齐居中
		headerCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
		// 背景色
		headerCellStyle.setFillForegroundColor(IndexedColors.WHITE1.getIndex());
		WriteFont headerFont = new WriteFont();
		headerFont.setFontHeightInPoints((short) 14);
		headerCellStyle.setWriteFont(headerFont);
		// 自动换行
		headerCellStyle.setWrapped(Boolean.FALSE);

		// 内容策略
		WriteCellStyle contentCellStyle = new WriteCellStyle();
		// 设置数据允许的数据格式,这里49代表所有可以都允许设置
		DataFormatData dataFormatData = new DataFormatData();
		dataFormatData.setIndex((short)49);
		contentCellStyle.setDataFormatData(dataFormatData);
		// 设置背景色: 需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
		contentCellStyle.setFillPatternType(FillPatternType.NO_FILL);
		contentCellStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
		// 设置内容靠左对齐
		contentCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
		// 设置字体
		WriteFont contentFont = new WriteFont();
		contentFont.setFontHeightInPoints((short) 12);
		contentCellStyle.setWriteFont(contentFont);
		// 设置自动换行
		contentCellStyle.setWrapped(Boolean.FALSE);
		// 设置边框样式和颜色
		contentCellStyle.setBorderLeft(BorderStyle.MEDIUM);
		contentCellStyle.setBorderTop(BorderStyle.MEDIUM);
		contentCellStyle.setBorderRight(BorderStyle.MEDIUM);
		contentCellStyle.setBorderBottom(BorderStyle.MEDIUM);
		contentCellStyle.setTopBorderColor(IndexedColors.BLACK1.getIndex());
		contentCellStyle.setBottomBorderColor(IndexedColors.BLACK1.getIndex());
		contentCellStyle.setLeftBorderColor(IndexedColors.BLACK1.getIndex());
		contentCellStyle.setRightBorderColor(IndexedColors.BLACK1.getIndex());

		// 将格式加入单元格样式策略
		return new HorizontalCellStyleStrategy(headerCellStyle, contentCellStyle);
	}
}
