package com.ycl.platform.domain.excel; 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 org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddressList; import java.util.List; /** * 设置easy-excel下拉处理器 * * @author:xp * @date:2024/3/15 17:27 */ public class PointSelectHandler implements CellWriteHandler { private List unitNameList; public PointSelectHandler(List unitNameList) { this.unitNameList = unitNameList; } @Override public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) { // 获取sheet对象 Sheet sheet = writeSheetHolder.getSheet(); // 获取数据校验helper,excel的下拉就是通过数据校验设置 DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper(); // 设置下拉范围,第一列,20000行内 CellRangeAddressList questionTypeRange = new CellRangeAddressList(1, 20000, 4, 4); // 添加单位下拉 DataValidationConstraint questionConstraint = dataValidationHelper.createExplicitListConstraint((String[]) unitNameList.toArray()); DataValidation questionValidation = dataValidationHelper.createValidation(questionConstraint, questionTypeRange); sheet.addValidationData(questionValidation); } }