Как реализовать Excel vbA в C # - PullRequest
1 голос
/ 15 июня 2009

Можете ли вы помочь мне переписать следующий код 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 ]

5 голосов
/ 15 июня 2009

Что вам нужно, так это Excel Automation. В значительной степени это означает использование набора COM-объектов, предоставленных Excel, для удаленного управления Excel из другого приложения.

Все, что вы можете сделать с VBA, вы можете достичь с помощью автоматизации (хорошо, почти все).

Если вы воспользуетесь Google для "Excel Automation C #", вы получите много хитов. Как автоматизировать Microsoft Excel из Microsoft Visual C # .NET был мне первым возвращен и выглядит как хорошее место для начала.

Надеюсь, это поможет,

2 голосов
/ 16 июня 2009
 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;
...