1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
/// <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); } |
1 2 3 4 5 6 7 8 |
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) } |
from:https://blog.csdn.net/a304954732/article/details/127220912