如何在C#中实现Excel vbA

时间:2022-06-19 17:54:21

Can you help me rewriting the following Excel VB code to C#?

你能帮我把以下Excel VB代码改写成C#吗?

Range("C9:E11").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
    Formula1:="=1"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
    .PatternColorIndex = xlAutomatic
    .Color = 255
    .TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False

2 个解决方案

#1


What you're looking for is Excel Automation. Pretty much this means using a set of COM objects provided by Excel to remote control Excel from another application.

您正在寻找的是Excel Automation。这意味着使用Excel提供的一组COM对象从另一个应用程序远程控制Excel。

Anything you can do with with VBA you can achieve with Automation (OK, almost anything).

使用VBA可以实现的任何功能都可以通过自动化实现(好的,几乎任何东西)。

If you google for "Excel Automation C#" you'll get lots of hits. How to automate Microsoft Excel from Microsoft Visual C# .NET was the first returned to me, and looks like a good place to get started.

如果您谷歌搜索“Excel Automation C#”,您将获得大量点击。如何从Microsoft Visual C#.NET自动化Microsoft Excel是第一次返回给我,看起来像一个开始的好地方。

Hope this helps,

希望这可以帮助,

#2


 using Excel = Microsoft.Office.Interop.Excel;
 ...
 object mis = Type.Missing;

 Excel.FormatCondition cond =
    (Excel.FormatCondition)range.FormatConditions.Add(Excel.XlFormatConditionType.xlCellValue,
    Excel.XlFormatConditionOperator.xlEqual, "=1",
    mis, mis, mis, mis, mis);
    cond.Interior.PatternColorIndex = Excel.Constants.xlAutomatic;
    cond.Interior.TintAndShade = 0;
    cond.Interior.Color = ColorTranslator.ToWin32(Color.White);
    cond.StopIfTrue = false;

#1


What you're looking for is Excel Automation. Pretty much this means using a set of COM objects provided by Excel to remote control Excel from another application.

您正在寻找的是Excel Automation。这意味着使用Excel提供的一组COM对象从另一个应用程序远程控制Excel。

Anything you can do with with VBA you can achieve with Automation (OK, almost anything).

使用VBA可以实现的任何功能都可以通过自动化实现(好的,几乎任何东西)。

If you google for "Excel Automation C#" you'll get lots of hits. How to automate Microsoft Excel from Microsoft Visual C# .NET was the first returned to me, and looks like a good place to get started.

如果您谷歌搜索“Excel Automation C#”,您将获得大量点击。如何从Microsoft Visual C#.NET自动化Microsoft Excel是第一次返回给我,看起来像一个开始的好地方。

Hope this helps,

希望这可以帮助,

#2


 using Excel = Microsoft.Office.Interop.Excel;
 ...
 object mis = Type.Missing;

 Excel.FormatCondition cond =
    (Excel.FormatCondition)range.FormatConditions.Add(Excel.XlFormatConditionType.xlCellValue,
    Excel.XlFormatConditionOperator.xlEqual, "=1",
    mis, mis, mis, mis, mis);
    cond.Interior.PatternColorIndex = Excel.Constants.xlAutomatic;
    cond.Interior.TintAndShade = 0;
    cond.Interior.Color = ColorTranslator.ToWin32(Color.White);
    cond.StopIfTrue = false;