Programming MS office with C# - is it possible?
Asked Answered
E

5

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?

Equable answered 23/3, 2016 at 8:5 Comment(3)
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...Gibeon
Is NPOI something like java API for Microsoft Documents (,which is for .net)?Equable
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.comGibeon
B
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.

Bulger answered 23/3, 2016 at 8:7 Comment(0)
S
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
Sinegold answered 23/3, 2016 at 8:42 Comment(0)
S
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.

Stature answered 26/3, 2016 at 23:51 Comment(0)
E
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.

Eckstein answered 23/3, 2016 at 19:58 Comment(0)
T
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.

Tabling answered 23/5, 2017 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.