package cn.report.excel.easyexcel.custom;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import cn.report.excel.easyexcel.api.write.handler.SheetWriteHandler;
import cn.report.excel.easyexcel.api.write.metadata.holder.WriteSheetHolder;
import cn.report.excel.easyexcel.api.write.metadata.holder.WriteWorkbookHolder;

/**设置第一行大标题样式
 * 使用：
 * //标题样式
 *  WriteCellStyle headWriteCellStyle = EasyExcelUtil.getHeadStyle();
 *	HorizontalCellStyleStrategy horizontalCellStyleStrategy =
        new HorizontalCellStyleStrategy(headWriteCellStyle, new WriteCellStyle());
 * 	String[] header = {"二级标题1","二级标题2"};
 *	String bigtitle= "一级标题";
 *  WriteSheet writeSheet = EasyExcel.writerSheet("模板")
				.head(EasyExcelUtil.head(header))
			   // 第一行大标题样式设置
                .registerWriteHandler(new SheetWriteHandlerUtil(bigtitle,header ))
               //设置默认样式及写入头信息开始的行数 标题占两行：一级标题和二级标题
                .useDefaultStyle(true).relativeHeadRowIndex(1)
               // 表头、内容样式设置
                .registerWriteHandler(horizontalCellStyleStrategy)
                //统一列宽
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
               https://blog.csdn.net/weixin_52582672/article/details/119993626
 */
public class SamTitleWriteHandler implements SheetWriteHandler {

    private final String title;
    private final String[] header;

    public SamTitleWriteHandler(String title, String[] header) {
        this.title = title;
        this.header = header;
    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        Workbook workbook = writeWorkbookHolder.getWorkbook();
        Sheet sheet = workbook.getSheetAt(0);
        //设置标题
        Row row1 = sheet.createRow(0);
        row1.setHeight((short) 800);
        Cell cell = row1.createCell(0);
        //设置单元格内容
        cell.setCellValue(title);
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        Font font = workbook.createFont();
        font.setBold(true);
        font.setFontHeight((short) 400);
        cellStyle.setFont(font);
        cell.setCellStyle(cellStyle);
        // 第一行大标题占位设置
        sheet.addMergedRegionUnsafe(new CellRangeAddress(0, 0, 0, header.length-1));
    }
}
