我正在尝试向第11列的单元格添加一个下拉列表。我使用了stackoverflow的代码段,但引发了异常
((Excel.Range)mysheet.Cells[row_counter, 10]).Value2 = loginname; //works
var list = new System.Collections.Generic.List<string>();
list.Add("okay");
list.Add("unknown");
list.Add("delete");
list.Add("else (Comment)");
var flatList = string.Join(",", list.ToArray());
var cell = (Excel.Range)mysheet.Cells[row_counter, 11];
cell.Validation.Delete();
cell.Validation.Add(
Excel.XlDVType.xlValidateList,
Excel.XlDVAlertStyle.xlValidAlertInformation,
Excel.XlFormatConditionOperator.xlBetween,
flatList , Type.Missing
);
cell.Validation.InCellDropdown = true;
cell.Validation.IgnoreBlank = true;
cell.Locked = false; // the whole sheet is protected, mysheet.Protect(UserInterfaceOnly: true);
未处理的异常是System.Runtime.InteropServices.COMException: 'Exception from HRESULT: 0x800A03EC'
。其他所有代码都可以正常工作。我读到命令的顺序很重要,但是我认为我正确地做到了。有人可以帮助我,哪里出了问题。