使用 VSTO C# 的 Excel 单元格下拉列表中的列表项是否有任何限制?
Is there any limit on list items in Excel cell dropdown using VSTO C#?
我正在使用 vsto
创建 excel 加载项。我正在向单元格添加验证并将其设为 Dropdown
。下拉列表的数据源存在于另一列中。我正在使用此代码。
string values = string.Join(",", cellValuesArray);
cellRange.Validation.Delete();
cellRange.Validation.Add(Excel.XlDVType.xlValidateList, Excel.XlDVAlertStyle.xlValidAlertStop,
Excel.XlFormatConditionOperator.xlBetween, values , Type.Missing);
cellRange.Validation.InCellDropdown = true;
现在如果 cellValuesArray
包含超过 1200(大约)个项目然后它会抛出一个错误说
An exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll but was not handled in user code
Additional information: Exception from HRESULT: 0x800A03EC
如果项目数量很少,如 600-700,它工作正常。我不确定如何解决这个问题。
那么我怎样才能以这种方式添加验证,以便我的下拉列表可以容纳超过 1K 条记录或可能比这多得多?
经过大量研究,我终于找到了解决方案。如果您想在 excel 单元格的下拉列表中获取大量数据,那么使用公式是比使用 List
项目更好的解决方案。
string formula = "='SheetName'!$B:$B$" + lastRowIndex;
cellRange.Validation.Add(Excel.XlDVType.xlValidateList, Excel.XlDVAlertStyle.xlValidAlertStop,
Excel.XlFormatConditionOperator.xlBetween, formula , Type.Missing);
我这里使用的是B
列,你可以相应地更改它。
我正在使用 vsto
创建 excel 加载项。我正在向单元格添加验证并将其设为 Dropdown
。下拉列表的数据源存在于另一列中。我正在使用此代码。
string values = string.Join(",", cellValuesArray);
cellRange.Validation.Delete();
cellRange.Validation.Add(Excel.XlDVType.xlValidateList, Excel.XlDVAlertStyle.xlValidAlertStop,
Excel.XlFormatConditionOperator.xlBetween, values , Type.Missing);
cellRange.Validation.InCellDropdown = true;
现在如果 cellValuesArray
包含超过 1200(大约)个项目然后它会抛出一个错误说
An exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll but was not handled in user code
Additional information: Exception from HRESULT: 0x800A03EC
如果项目数量很少,如 600-700,它工作正常。我不确定如何解决这个问题。
那么我怎样才能以这种方式添加验证,以便我的下拉列表可以容纳超过 1K 条记录或可能比这多得多?
经过大量研究,我终于找到了解决方案。如果您想在 excel 单元格的下拉列表中获取大量数据,那么使用公式是比使用 List
项目更好的解决方案。
string formula = "='SheetName'!$B:$B$" + lastRowIndex;
cellRange.Validation.Add(Excel.XlDVType.xlValidateList, Excel.XlDVAlertStyle.xlValidAlertStop,
Excel.XlFormatConditionOperator.xlBetween, formula , Type.Missing);
我这里使用的是B
列,你可以相应地更改它。