简体   繁体   English

无法使用匹配公式 C# 在 Excel 中设置数据验证

[英]Cannot set Data Validation in Excel by using MATCH formula C#

After use MATCH formula to validationStr, my class went wrong.使用 MATCH 公式对 validationStr 后,我的 class 出错了。 Can anyone tell me why error happened?谁能告诉我为什么会发生错误? ERROR: System.Runtime.InteropServices.COMException: 'Exception from HRESULT: 0x800A03EC'错误:System.Runtime.InteropServices.COMException:“来自 HRESULT 的异常:0x800A03EC”

Thank you very much!非常感谢你!

using Microsoft.Office.Interop.Excel;

string validationListStr = "=OFFSET($AB$12,1,MATCH($T$12,$AB$12:$AD$12,0)-1,COUNTA(OFFSET($AB$12,1,MATCH($T$12,$AB$12:$AD$12,0)-1,100,1)),1)";
workSheet.Cells[1, 1].Validation.Delete();
workSheet.Cells[1, 1].Validation.Add(XlDVType.xlValidateList, XlDVAlertStyle.xlValidAlertInformation, Type.Missing, validationListStr, Type.Missing);
workSheet.Cells[1, 1].Validation.IgnoreBlank = true;
workSheet.Cells[1, 1].Validation.InCellDropdown = true;

I set the formula to validation by hand in Excel. There was no problem.我在Excel把公式设置为手工验证,没有问题。 However, I cannot set the formula by C#.但是,我无法通过 C# 设置公式。

The wrong 'Missing' object is being passed to the Add function.错误的“失踪”object 被传递给地址 function。

System.Type.Missing needs to be changed to System.Reflection.Missing.Value System.Type.Missing需要改为System.Reflection.Missing.Value

workSheet.Cells[1, 1].Validation.Add(
    XlDVType.xlValidateList, 
    XlDVAlertStyle.xlValidAlertInformation, 
    Missing.Value, // <-
    validationListStr, 
    Missing.Value // <-
);

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

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