sss
sss

Reputation:

How to implement Excel vbA in C#

Can you help me rewriting the following Excel VB code to 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

Upvotes: 1

Views: 5642

Answers (2)

sss
sss

Reputation:

 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;

Upvotes: 2

Binary Worrier
Binary Worrier

Reputation: 51739

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.

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

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.

Hope this helps,

Upvotes: 5

Related Questions