我用 C# 和 Microsoft.Office.Interop.Excel
编写代码。在生成的下拉列表中,可以在“确定”、“删除”、“其他”之间进行选择。现在,如果选择“删除”,我希望单元格变为红色。我试着用 Excel.FormatCondition
来做到这一点:
Excel.FormatCondition condition = mysheet.get_Range("J2:J10",
Type.Missing).FormatConditions.Add(Type:Excel.XlFormatConditionType.xlTextString,
Operator: Excel.XlFormatConditionOperator.xlEqual, Formula1: "=\"delete\"");
condition.Interior.ColorIndex = 3;
使用这段代码我得到一个错误:
System.Runtime.InteropServices.COMException: 'Exception from HRESULT: 0x800A03EC'
最佳答案
老问题,但希望这可能对其他人有所帮助。
当您将类型设置为 Type:Excel.XlFormatConditionType.xlTextString
时,您需要设置 String:
和 TextOperator:
参数,而不是 Operator:
和 Formula1:
如问题所示。
修复代码:
FormatConditions.Add(Type:Excel.XlFormatConditionType.xlTextString,
TextOperator: Excel.XlContainsOperator.xlContains, String: "delete");
关于基于单元格文本的 Microsoft.Office.Interop.Excel.Cells 上的 C# 条件格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56690072/