簡體   English   中英

使用驗證時 Excel 下拉列表的許多項目。從 c# 添加

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

在我創建 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);

對於這一行代碼,調試器會拋出錯誤:

System.Runtime.InteropServices.COMException HResult=0x800A03EC 消息=來自 HRESULT 的異常:0x800A03EC 源=無法評估異常源 StackTrace:無法評估異常堆棧跟蹤

我發現這是因為下拉列表的值太多,它們存儲在 Validation.Add() function 的值變量中。

添加驗證時如何克服下拉列表的值數量限制?

這是解決方案(感謝@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