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

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Sheet;

import java.util.Map;

/**
 * 自定义列宽度
 * @author vinjor-M
 * @date 9:26 2024/8/4
**/
@Slf4j
public class SetColumnWidthHandler implements SheetWriteHandler {
    private Map<Integer, Integer> columnWidthMap;

    public SetColumnWidthHandler(Map<Integer, Integer> columnWidthMap) {
        this.columnWidthMap = columnWidthMap;
    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        for (Map.Entry<Integer, Integer> entry : columnWidthMap.entrySet()) {
            // Excel中宽度的单位是1/256个字符宽度
            writeSheetHolder.getSheet().setColumnWidth(entry.getKey(), entry.getValue() * 256);
        }
    }
}
