Search code examples
c#.netexcelcom-automation

Run/drive Excel via own application


Is it possible to 'drive' excel via a c# application (i.e. select something from a excel gui dropdown list, press a button and read the content of particular cells)?

This is not really for testing but for data scrapping similarly to selenium where you can drive a browser via C#.


Solution

  • Is it possible to 'drive' excel via a c# application (i.e. select something from a excel gui dropdown list, press a button and read the content of particular cells)?

    Yes, you can use COM Automation from C#. Create a C# project and add a reference in the COM section of the dialog. You should reference Microsoft Excel 14.0 Object Library. This is the Excel 2010 version and if you use another version you should reference the version you have installed on your machine.

    You can then automate Excel in a similar way as you would do using VBA except you now are using C#. On MSDN you have Getting Started with VBA in Excel 2010 that among other things explain how you can use the macro recorder to create a VBA subroutine from actions you perform in Excel. You then have to translate the VBA into similar C# code.