package org.sky.scientific.excel.handle;

import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import org.apache.poi.ss.usermodel.*;

public class CustomCellStyleHandler implements CellWriteHandler {

//	private int maxHeadIndex;
//
//	public CustomCellStyleHandler(int maxHeadIndex) {
//		super();
//		this.maxHeadIndex = maxHeadIndex;
//	}

	@Override
	public void afterCellDispose(CellWriteHandlerContext context) {

		Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();

		Sheet sheet = workbook.getSheetAt(0);
		// 获取单元格
		Cell cell = context.getCell();
		//必须这么获取，否则设置的样式不生效
		CellStyle cellStyle = workbook.createCellStyle();

		// 设置水平和垂直居中
		cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
		cellStyle.setAlignment(HorizontalAlignment.CENTER);
		cellStyle.setFillBackgroundColor(IndexedColors.WHITE.getIndex());
		cellStyle.setBorderTop(BorderStyle.THIN);
		cellStyle.setBorderBottom(BorderStyle.THIN);
		cellStyle.setBorderLeft(BorderStyle.THIN);
		cellStyle.setBorderRight(BorderStyle.THIN);

		if(cell.getRowIndex() == 0 ){
			sheet.getRow(0).setHeightInPoints(30);
		}

		Font font = workbook.createFont();
		font.setBold(cell.getRowIndex() <= 2);
		font.setFontHeight((short) 240);//11*20   实际字高*20
		font.setFontName("宋体");
		cellStyle.setFont(font);
		// 设置样式回到单元格
		cell.setCellStyle(cellStyle);
	}

	@Override
	public int order() {
		return 1000000;
	}
}
