package cn.iocoder.yudao.framework.excel.core.handler;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;

import java.util.List;

/**
 * 自定义列样式
 * @author vinjor-M
 * @date 9:48 2024/8/4
**/
@Slf4j
public class CellTextWrapHandler implements CellWriteHandler {
    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {
        // 在这里可以设置是否为头部，根据需要决定是否要为头部单元格设置样式
        if (!isHead) {
            Cell cell = row.createCell(columnIndex);
            CellStyle style = writeSheetHolder.getSheet().getWorkbook().createCellStyle();
            style.setWrapText(true); // 设置自动换行
            cell.setCellStyle(style);
        }
    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        // 可以在这里设置合并单元格等操作
    }
}
