如何使用C#为Excel创build“文本包含”FormattingConditional(格式条件)

我想要的是能够根据其中的文本值来改变一些Excel文件的单元格的颜色。

这是我有什么:

private void validator(Excel.Worksheet sheet, int lastCellRowNum, XlRgbColor color) { FormatCondition cond = sheet.get_Range("A1:I"+lastCellRowNum,Type.Missing).FormatConditions.Add(XlFormatConditionType.xlCellValue, XlFormatConditionOperator.xlEqual, sheet.Cells[1,1]); cond.Interior.Color = color; } 

这段代码将Cell [1,1]的确切值与其他值进行比较,而不仅仅是一个包含的string。

基本上,我想要的是一种格式,允许“包含”条件,以提高我的代码的性能。 例如,如果在Cell [1,1] .Value2是“Hello”,我希望Value2等于“ByeHelloBye”的任何单元格或包含“Hello”的任何其他string包含在条件中。

现在我不得不多次调用这个方法30多倍,因为我不知道如何使这个条件。 将所有的格式应用到70.000行需要35秒。 太多了。

对我的问题的其他可能的解决scheme将是:

  1. 将整个二维数组的颜色传递给Excel。

对不起我的英文,并提前感谢。

所以朋友find了这个可怕的问题的答案。 允许这个“Contains”格式的函数实际上存在。 这是如何完成的:

  FormatCondition cond = sheet.get_Range("A1:I70000", Type.Missing).FormatConditions.Add(XlFormatConditionType.xlTextString, Type.Missing, Type.Missing, Type.Missing, "SomethingToFilterIfContained", XlContainsOperator.xlContains, Type.Missing, Type.Missing); cond.Interior.Color = color; 

我希望这可以帮助别人。