如何在 C# 中实现 Excel vbA

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/996643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:29:17  来源:igfitidea点击:

How to implement Excel vbA in C#

c#excel-vbaexcel-2007vbaexcel

提问by Binary Worrier

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

回答by Binary Worrier

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 自动化。这几乎意味着使用 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# .NETwas 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,

希望这可以帮助,

回答by Binary Worrier

 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;