简体   繁体   English

使用验证时 Excel 下拉列表的许多项目。从 c# 添加

[英]To many items for Excel drop down list when using validation.add from c#

In my c# project, which creates xlsx file, there is a rows in code, which deletes and then sets validation for a range of cells:在我创建 xlsx 文件的 c# 项目中,代码中有一行,它删除然后设置对一系列单元格的验证:

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);

For this row of code debugger throws an error:对于这一行代码,调试器会抛出错误:

System.Runtime.InteropServices.COMException HResult=0x800A03EC Message=Exception from HRESULT: 0x800A03EC Source= Cannot evaluate the exception source StackTrace: Cannot evaluate the exception stack trace System.Runtime.InteropServices.COMException HResult=0x800A03EC 消息=来自 HRESULT 的异常:0x800A03EC 源=无法评估异常源 StackTrace:无法评估异常堆栈跟踪

As I found out this is because there are too many values for a drop down list, which are stored in values variable of Validation.Add() function.我发现这是因为下拉列表的值太多,它们存储在 Validation.Add() function 的值变量中。

How can I overcome the limit on the number of values for a drop-down list when adding validation?添加验证时如何克服下拉列表的值数量限制?

Here is the solution(thanks to @Rory):这是解决方案(感谢@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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM