将VBA代码转换为C# [英] Converting VBA code to C#

查看:519
本文介绍了将VBA代码转换为C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VSTO中,Excel按钮点击事件很有效,但由于excelApp.Workbooks.Add()创建了一个新的工作簿?

In VSTO Excel button click event works great but because of the excelApp.Workbooks.Add() create a new workbook?

使用   Excel = Microsoft.Office.Interop.Excel;

class   程序

{      static   void   Main( string []
args)

    {

class Program
{    static void Main(string[] args)
    {



    }

}

问题是我不想创建新工作簿,我想在活动表上执行代码......

Problem is I don't want to create a new workbook, I want to execute code on activesheet...

推荐答案

您好,

您可以使用Application类的 ActiveWorbook 属性,而不是添加新工作簿:

You can use the ActiveWorbook property of the Application class instead of adding a new workbook:

             var excelApp = new Excel.Application();
            
             //Insert VBA code here.
             excelApp.ActiveCell.FormulaR1C1 = "1";
             excelApp.Range["A2"].Select();
             excelApp.ActiveCell.FormulaR1C1 = "2";
             excelApp.Range["A1:A2"].Select();
             excelApp.Selection.AutoFill(Destination: excelApp.Range["A1:A10"], Type: Excel.XlAutoFillType.xlFillDefault);
             excelApp.Range["A1:A10"].Select();

             excelApp.Selection.Interior.Pattern = Excel.Constants.xlSolid;
             excelApp.Selection.Interior.PatternColorIndex = Excel.Constants.xlAutomatic;
             excelApp.Selection.Interior.ThemeColor = Excel.XlThemeColor.xlThemeColorAccent1;
             excelApp.Selection.Interior.TintAndShade = 0.399945066682943;
             excelApp.Selection.Interior.PatternTintAndShade = 0;

             excelApp.Range["A1:A10"].Select();
             excelApp.ActiveSheet.Shapes.AddChart.Select();
             excelApp.ActiveChart.ChartType = Excel.XlChartType.xlConeColStacked;
             excelApp.ActiveChart.SetSourceData(Source: excelApp.Range["Sheet1!


A


1:


这篇关于将VBA代码转换为C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆