我正在尝试使用 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;
我动态地更改这些格式的应用范围
formatConditionObj.ModifyAppliesToRange(NewRange);
现在我想删除应用的这种格式,如何实现。
formatConditionObj.Delete();
这对我不起作用。这不会删除应用该格式的所有单元格的格式。仅删除最后的单元格格式。
我也尝试过使用
formatConditionObj.AppliesTo.Delete();
但它也会删除应用于该单元格的其他条件格式。
注意:某些格式已应用于应用此条件格式的单元格,例如某些填充颜色。甚至还有一些其他条件格式应用于某些单元格。我只想删除这个特定的 ConditionalFormat(formatConditionObj)。
谁能帮我。
当单元格中有多个条件时,无法删除这样的格式条件。您必须通过编号来解决条件格式才能将其删除。
考虑这个例子。 (经过测试和尝试)
以下代码创建一个新工作簿,并在工作表 1 中的单元格 A1 中创建 2 个格式条件。创建 2 个条件后,应用程序将暂停并显示一个消息框。转到 Excel 并手动检查创建的条件格式。 (快照 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
}
快照1
快照2
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)