删除条件格式

我想用下面的代码添加使用C#的条件格式。

Microsoft.Office.Interop.Excel.FormatCondition formatConditionObj = null; formatConditionObj = (Microsoft.Office.Interop.Excel.FormatCondition)myRange .FormatConditions.Add(Excel.XlFormatConditionType.xlExpression, Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); formatConditionObj.Interior.ColorIndex = 5; 

dynamic我改变这些格式应用的范围

 formatConditionObj.ModifyAppliesToRange(NewRange); 

现在我想删除这个应用的格式,这个如何实现。

 formatConditionObj.Delete(); 

这对我不起作用。 这不会删除应用它的所有单元格的格式。 只有最后一个单元格格式被删除。

我也试过使用

 formatConditionObj.AppliesTo.Delete(); 

但是它也删除了其他适用于该单元格的ConditionalFormats。

注意:某些格式已应用于应用了这种条件格式的单元格,例如一些填充颜色。 即使在某些单元格上还应用了其他一些条件格式。 我只想删除这个特定的ConditionalFormat(formatConditionObj)。

谁能帮我。

当单元格中有多个条件时,不能删除这样的格式条件。 你必须通过编号来删除条件格式。

考虑这个例子。 ( testing和试用

下面的代码创build一个新的工作簿,在工作表1中在单元格A1中创build2个格式条件。 在创build了2个条件之后,应用程序将通过显示一个消息框来暂停。 转到Excel并手动检查创build的条件格式。 (快照1)。 完成后,在消息框中单击OK 。 然后代码将删除条件1,然后通过显示消息框再次暂停。 转到Excel并手动检查条件格式。 你会注意到只剩下一个(第二个是精确的)条件格式。 (快照2)

  private void btnSearch_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Excel.Application xlexcel; Microsoft.Office.Interop.Excel.Workbook xlWorkBook; Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet; xlexcel = new Excel.Application(); xlexcel.Visible = true; //~~> Add a File xlWorkBook = xlexcel.Workbooks.Add(); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); //~~> Create 2 Conditions xlWorkSheet.Cells[1, 1].FormatConditions.Add( 1,5,"=5"); xlWorkSheet.Cells[1, 1].FormatConditions.Add(1, 5, "=10"); MessageBox.Show("Wait"); //~~> Now if you check the Excel file, Cell A1 has two conditional formats. //~~> See Snapshot 1 //~~> Delete the first condition xlWorkSheet.Cells[1, 1].formatconditions(1).delete(); MessageBox.Show("Wait"); //~~> Now if you check the Excel file, Cell A1 has only 1 conditional format. //~~> See Snapshot 2 } 

SNAPSHOT 1

在这里输入图像说明

SNAPSHOT 2

在这里输入图像说明