1、添加下拉框,并开启列值校验
/// <summary>
/// 添加下拉框,并开启列值校验
/// </summary>
public static void SetCellDropdownList(XSSFSheet sheet, int firstcol, int lastcol, List<string> vals)
{
//设置 下拉框内容
var dvHelper = new XSSFDataValidationHelper(sheet);
var dvConstraint = (XSSFDataValidationConstraint)dvHelper.CreateExplicitListConstraint(vals.ToArray());
//设置下拉框对应的行范围、列范围,行和列都是从0开始
var addressList = new CellRangeAddressList(0, 65535, firstcol, lastcol);
var validation = (XSSFDataValidation)dvHelper.CreateValidation(dvConstraint, addressList);
//开启下拉框
validation.ShowPromptBox = true;
//开启错误提醒
validation.ShowErrorBox = true;
sheet.AddValidationData(validation);
}
2、调用方
public static void main()
{
var workbook = new XSSFWorkbook();
XSSFSheet sheet = (XSSFSheet)workbook.CreateSheet("sheet1");
var validateList=new List<string>(){"选项1","选项2","选项3"};
SetCellDropdownList(sheet, 2, 2, validateList)
}