5

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?

3
  • 1
    I think, there a two general directions. First, programming a kind of plugin to add some functionalities inside Excel (or other). That can be done with e.g. VSTO, like Patrick said. Second one is a program to manipulate excel files witout excel itself. That could be done with NPOI, or one of several other libraries out there... Commented Mar 23, 2016 at 8:27
  • Is NPOI something like java API for Microsoft Documents (,which is for .net)?
    – Fsalad
    Commented Mar 23, 2016 at 8:33
  • 1
    NPOI is a library that gives you classes/methods to manipulate excel or word files direct from a c# application. Take a look at npoi.codeplex.com Commented Mar 23, 2016 at 8:41

5 Answers 5

5

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.

5

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:

  1. Excel
  2. Word
5

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.

3

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.