基于单元格文本的 Microsoft.Office.Interop.Excel.Cells 上的 C# 条件格式

标签 c# excel-formula excel-interop

我用 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/

相关文章:

C# 在 Excel 文件中将文本序列号作为日期写入

c# - 用多线程写入excel文件

c# - Excel 卡在消息框询问是否要保存工作簿的位置

c# - 如何将UpdateSourceTrigger的值交给UserControl或者运行时更新?

C# PictureBox 控件闪烁/性能问题

Excel 使用基于用户语言的函数

excel - 生成协方差矩阵的更快方法

c# - 如何在 ListView 中访问 WebView 的 NavigateToString 属性

c# - 是否可以使用 HTTP 连接池?

excel - 使用另一个公式提取公式的参数