package jnpf.handler;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.*;

public class CustomCellStyleHandler implements CellWriteHandler {
    private CellStyle headerStyle;
    private CellStyle contentStyle;

    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
        // 初始化表头样式（只创建一次）
        if (headerStyle == null) {
            headerStyle = workbook.createCellStyle();
            headerStyle.setAlignment(HorizontalAlignment.CENTER);
            headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            Font headerFont = workbook.createFont();
            headerFont.setBold(true);
            headerFont.setFontName("黑体");
            headerFont.setFontHeightInPoints((short) 12);
            headerStyle.setFont(headerFont);
        }
        // 初始化内容样式（只创建一次）
        if (contentStyle == null) {
            contentStyle = workbook.createCellStyle();
            contentStyle.setAlignment(HorizontalAlignment.CENTER);
            contentStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        }
        // 根据是否为表头应用对应的样式
        if (isHead) {
            cell.setCellStyle(headerStyle);
        } else {
            cell.setCellStyle(contentStyle);
        }
    }
}