C# excel数据验证下拉列表抛出错误

Cha*_*ake 3 .net c# excel

我正在尝试使用 C# 将下拉列表添加到范围。

这就是我到目前为止所做的。

 Worksheet ws = PPTAddIn.thisAddin2Obj.Application.ActiveWorkbook.ActiveSheet;
            ws.get_Range("a1").Validation.Delete();
            ws.get_Range("a1").Validation.InCellDropdown = true;
            ws.get_Range("a1").Validation.IgnoreBlank = true;
            ws.get_Range("a1").Validation.Add(XlDVType.xlValidateList, XlDVAlertStyle.xlValidAlertWarning, "opt1,opt2,opt3", Missing.Value);
Run Code Online (Sandbox Code Playgroud)

在代码的第三行,它抛出以下异常
Exception from HRESULT: 0x800A03EC

错误图像

这是堆栈跟踪

   at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)    at Microsoft.Office.Interop.Excel.Validation.set_InCellDropdown(Boolean ) at MS.ProductionPlanningTool.Excel.Ribbon_PPT.ribbon_signin_Click(Object sender, RibbonControlEventArgs e) in D:\MidasCloud\CloudTFS\ProductionPlanning\MSP2\MS.ProductionPlanningTool.Excel\UI\Ribbon_PPT.cs:line 1328    at Microsoft.Office.Tools.Ribbon.RibbonPropertyStorage.ControlActionRaise(IRibbonControl control)    at Microsoft.Office.Tools.Ribbon.RibbonPropertyStorage.ButtonClickCallback(RibbonComponentImpl component, Object[] args)    at Microsoft.Office.Tools.Ribbon.RibbonManagerImpl.Invoke(RibbonComponentCallback callback, Object[] args)    at Microsoft.Office.Tools.Ribbon.RibbonMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)    at Microsoft.Office.Tools.Ribbon.RibbonManagerImpl.System.Reflection.IReflect.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParameters)
Run Code Online (Sandbox Code Playgroud)

TnT*_*nMn 5

命令顺序不正确。将Add语句移至紧接该Delete语句之后。Validation如果不存在验证,则无法在对象上设置其他值。

Worksheet ws = PPTAddIn.thisAddin2Obj.Application.ActiveWorkbook.ActiveSheet;
ws.get_Range("a1").Validation.Delete();
ws.get_Range("a1").Validation.Add(XlDVType.xlValidateList, XlDVAlertStyle.xlValidAlertWarning, "opt1,opt2,opt3", Missing.Value);
ws.get_Range("a1").Validation.InCellDropdown = true;
ws.get_Range("a1").Validation.IgnoreBlank = true;
Run Code Online (Sandbox Code Playgroud)

边注:

使用 Excel 中的宏记录器获取操作的语法。虽然输出采用 VBA 格式,但语法并没有太大不同,您无法破译命令并将其转换为 C#。VBA 与 VB.Net 足够接近,VB 到 C# 转换器将生成您可以清理和使用的代码。唯一的问题是宏记录器广泛使用该Selection对象,您应该将其转换为该Selection对象代表的任何内容(很可能是一个Excel.Range)。您还需要将 VB 的索引属性更正为等效方法调用(即:Range("a1")-> get_Range("a1"))。

编辑: 我最初没有检查你的Add语句的语法,但看来你缺少一个参数。

文档中:

void Add(
    XlDVType Type,
    Object AlertStyle,
    Object Operator,
    Object Formula1,
    Object Formula2
)
Run Code Online (Sandbox Code Playgroud)

和你的Add声明:

Add(XlDVType.xlValidateList, 
    XlDVAlertStyle.xlValidAlertWarning, 
    "opt1,opt2,opt3", 
    Missing.Value)
Run Code Online (Sandbox Code Playgroud)

您似乎缺少该Operator参数。使用宏记录器,默认值似乎是XlFormatConditionOperator.xlBetween

您还应该定义一个Validation对象并使用它而不是ws.get_Range("a1").Validation设置/调用每个属性/方法。