简体   繁体   English

使用C#在Excel工作表中运行宏

[英]Run a macro in excel sheet using c#

Hi friends i am working on vss 2012, Excel 2013. I am trying to implement the Exporting of Grid view records to the Excel sheet. 嗨,朋友们,我正在研究vss 2012,Excel2013。我正在尝试将表格视图记录导出到Excel工作表。 Excel sheet would be in good style. Excel工作表将是很好的样式。 So what i did is i created a Macro of my design in one excel sheet. 所以我要做的是在一个Excel工作表中创建了我的设计宏。 So now i have the code of that particular macro. 所以现在我有了那个特定宏的代码。

Now i want to use this macro(Design) in new excel sheets also. 现在我也想在新的Excel工作表中使用此宏(设计)。 Like say every day new records will be added to the grid view, and at the end of the day if i click on export to excel ia new excel should get created with the design of macro, and all records. 就像说每天都会将新记录添加到网格视图中一样,在一天结束时,如果我单击“导出到excel”,那么应该使用宏和所有记录的设计来创建新的excel。 Is that possible?? 那可能吗??

What could be the difficulties in implementing this .. I could not find anything related to this in net. 实施此过程可能会有什么困难。.我在网上找不到与此相关的任何内容。 So please if anybody know this please let me know... 所以,如果有人知道这一点,请告诉我...

You can insert VBA macro using this code: 您可以使用以下代码插入VBA宏:

    public void SetVBACode(string _macro)
    {
        VBProject project = workbook.VBProject;
        VBComponents components = project.VBComponents;           
        VBComponent component =components.Add(vbext_ComponentType.vbext_ct_StdModule);
        component.CodeModule.AddFromString(_macro);
    }

And run them using this method: 并使用以下方法运行它们:

    public void RunVBACode(string _name)
    {
        excelApplication.RunVBACode(_name);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM