|
各位好,我实现EXCEL单元格的聚光灯效果,参考了一下VBA的代码
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
On Error Resume Next
MsgBox Cells.Parent.Parent.Parent.Name
Application.Cells.FormatConditions.Delete
With Target.EntireRow.FormatConditions
.Delete
.Add xlExpression, , "TRUE"
.Item(1).Interior.ColorIndex = 40
End With
With Target.EntireColumn.FormatConditions
.Delete
.Add xlExpression, , "TRUE"
.Item(1).Interior.ColorIndex = 37
End With
With Target.FormatConditions
.Delete
.Add xlExpression, , "TRUE"
.Item(1).Interior.ColorIndex = 2
End With
End Sub
这段VBA代码运行是正常的,
改写成C#为
public void ChangeColor(Excel.Range Target)
{
Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet.Cells.FormatConditions.Delete();
Target.EntireColumn.FormatConditions.Delete();
Target.EntireColumn.FormatConditions.Add(Type: Excel.XlFormatConditionType.xlExpression, Formula1: "TRUE");
Target.EntireColumn.Item[1].Interior.ColorIndex = 40; //设置当前列颜色
Target.EntireRow.FormatConditions.Delete();
Target.EntireRow.FormatConditions.Add(Type: Excel.XlFormatConditionType.xlExpression, Formula1: "TRUE");
Target.EntireRow.Item[1].Interior.ColorIndex = 37; //设置当前行颜色
Target.FormatConditions.Delete();
Target.FormatConditions.Add(Type: Excel.XlFormatConditionType.xlExpression, Formula1: "TRUE");
Target.Item[1].Interior.ColorIndex = 2; //设置当前单元格颜色
}
可是第一句
Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet.Cells.FormatConditions.Delete();
却始终没有像想象中运行,请教一下这是为什么呢?
|
|