简体   繁体   中英

Excel and Macro using C#

Sorry if this is a dumb question but I didn't found any help on google.

I'm looking for a help to

  1. Open Excel file
  2. Paste some string into into (A1)
  3. Click on a macro in the excel file.

Plzz help me out gyus... Thx....

The Microsoft Open XML API is available from here https://msdn.microsoft.com/en-us/library/office/bb448854.aspx . This .net library allows you to open any OpenXML based Office documents in C#, edit them and save them.

You dont even need a copy of Excel installed to use it.

I found the solution...

add using Excel = Microsoft.Office.Interop.Excel; to your code and click on add references and under COM you will find "Microsoft Excel 16.0 Object Library" I'm using office 2016 so the no may differ based on the office version.

Here goes the code to open excel.

        Excel.Application xlApp = new Excel.Application();
        xlApp.Visible = true;
        Excel.Workbook xlWorkBooks;
        xlWorkBooks = xlApp.Workbooks.Open(@"F:\Book1.xlsm", true , false);//This is added to avoid the opening in ReadOnly mode


        xlApp.Run("'" + @"F:\Book1.xlsm"+ "'!" + "Macro1");//The middle part is excel location and the last part is macro name in that excel file

Hope you guys got a help... Thx for votes...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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