MS office apps like Excel can be programmed with Visual Basic for Applications.Is it possible to program MS office with C# for advanced tasks?
Yes, it is. You can use Visual Studio Tools for Office, which allows add-ins to be written in C#, VB.NET, etc. It uses COM interop to communicate with Office.
Also, nowadays you have Office apps, which are a hybrid between JavaScript and possibly a server endpoint, which can be coded in C# too.
Definitely. You can install Visual Studio Community 2015 for free. Then look into this folder (path may vary):
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Visual Studio Tools for Office\PIA\Office15
From here you can find dll for each Office app.
Taking Microsoft.Office.Interop.Excel.dll
for example, start a new C# project, add that dll into project reference.
using Excel = Microsoft.Office.Interop.Excel;
Then you can initialize your excel app like this:
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"C:\path\file.xlsx");
Excel.Worksheet xlWorksheet = xlWorkbook.Sheets[1];
There is a abundance of these tutorials online. DoNotPerls is a good place to start with basic:
It would be an injustice not to mention Excel-DNA. An amazing product written by a talented and very helpful chap and with good community support. It is available at no charge and makes it very easy to make Excel custom functions, RTD servers and other goodies.
Excel-DNA is an independent project to integrate .NET into Excel. With Excel-DNA you can make native (.xll) add-ins for Excel using C#, Visual Basic.NET or F#, providing high-performance user-defined functions (UDFs), custom ribbon interfaces and more. Your entire add-in can be packed into a single .xll file requiring no installation or registration.
Writing a custom function is as easy as writing a static method and including the compiled DLL or .cs file with the provided XLL file.
Another great option (and the one I recommend) is to use Add-in Express. https://www.add-in-express.com
Their toolset greatly extends what is possible with VSTO or COM add-ins. Their support is fantastic and their site has examples for almost any Office development scenario.
For Excel you can use my ESharper add-in to interactively write lightweight user defined functions and commands using C# in a live Excel session without additional add-in deployment overhead.
© 2022 - 2024 — McMap. All rights reserved.