使用validation.add从c#添加到Excel的多个项目下拉列表中



在我的c#项目中,它创建xlsx文件,代码中有一行,它删除并设置一系列单元格的验证:

VedomostSheet.Range[VedomostSheet.Cells[VedomostStartRow, Convert.ToInt16(GetCellValue(rowDic, 1, DicSheet))], VedomostSheet.Cells[20000, Convert.ToInt16(GetCellValue(rowDic, 1, DicSheet))]].Validation.Add(XlDVType.xlValidateList, XlDVAlertStyle.xlValidAlertStop, XlFormatConditionOperator.xlBetween, values, Type.Missing);

对于这一行代码,调试器抛出一个错误:

System.Runtime.InteropServices.COMException HResult=0x800A03EC消息=HRESULT:0x800A03EC出现异常源=无法评估异常源StackTrace:无法评估异常堆栈跟踪

我发现这是因为下拉列表中的值太多,这些值存储在Validation.Add((函数的值变量中。

添加验证时,如何克服下拉列表的值数限制?

以下是解决方案(感谢@Rory(:

Range myrange;//creating the range variable with all values for drop down list
myrange = DicSheet.Range[DicSheet.Cells[2, Convert.ToInt16(GetCellValue(rowDic, 2, DicSheet))], DicSheet.Cells[row-1, Convert.ToInt16(GetCellValue(rowDic, 2, DicSheet))]];
//using this variable with the name of a excel sheet:
VedomostSheet.Range[VedomostSheet.Cells[VedomostStartRow, Convert.ToInt16(GetCellValue(rowDic, 1, DicSheet))], VedomostSheet.Cells[20000, Convert.ToInt16(GetCellValue(rowDic, 1, DicSheet))]].Validation.Add(XlDVType.xlValidateList, XlDVAlertStyle.xlValidAlertStop, XlFormatConditionOperator.xlBetween, "='" + DicSheet.Name + "'!" + myrange.Address, Type.Missing);

最新更新