We continue with our series of posts aimed at seeing how to interact with different programs through their API from a NET6 application written in C#.
In the previous post, we saw how to connect to Microsoft Word and perform actions with it through a NET6 application. We mentioned that one of the frequent requirements of IoT is the integration of technologies. Among them, program APIs occupy a particularly important place.
We started with the API for Microsoft Office applications because, as expected from Microsoft itself, they are very well done and documented. Today, it’s time to see how to connect an Excel application.
For example, imagine we could use an Excel sheet to record the values from a sensor measurement connected to an ESP32, create a series of charts, and finally save this report in a file folder.
However, I must remind you again that, although it is an interesting option, it does not mean it is the best option. For example, it wouldn’t be very correct to use an Excel sheet simply as a database. Or, to avoid having to create a dashboard with charts.
Each project should analyze what the best option is, from the global perspective of the project. In most projects, using the API to connect to Excel will not be the best option. But, sometimes, it can be a project requirement, and therefore it is interesting to be able to do it.
How to Connect to Microsoft Excel with a NET6 Application
Connecting to the Excel API is going to be very similar to the example we saw for Microsoft Word. First, we will create a simple NET6 console application.
Next, we must add references to the Microsoft Excel-specific assemblies. In the ‘COM’ references tab, look for “Microsoft Excel xx.0 Object Library”, where xx will be the version of Microsoft Excel you have installed on your computer.
Then we replace the code with the following:
using System.Reflection;
//Start Microsoft.Office.Interop.Excel and get Application object.
var applicationExcel = new Microsoft.Office.Interop.Excel.Application();
applicationExcel.Visible = true;
//Get a new workbook.
var workbook = (Microsoft.Office.Interop.Excel._Workbook)(applicationExcel.Workbooks.Add(Missing.Value));
var worksheet = (Microsoft.Office.Interop.Excel._Worksheet)workbook.ActiveSheet;
//Add table headers going cell by cell.
worksheet.Cells[1, 1] = "A";
worksheet.Cells[2, 1] = "B";
worksheet.Cells[3, 1] = "C";
worksheet.Cells[4, 1] = "D";
//Fill B1:B4 with a formula(=RAND() * 100000) and apply format.
var range = worksheet.get_Range("B1", "B4");
range.Formula = "=RAND() * 100000";
range.NumberFormat = "$0.00";
applicationExcel.Visible = true;
applicationExcel.UserControl = true;
Console.ReadLine();
This is a summarized and corrected version of Microsoft’s own examples available at this link: https://learn.microsoft.com/es-es/previous-versions/office/troubleshoot/office-developer/automate-excel-from-visual-c
If we run the code, we will see that a new Microsoft Excel application starts, fills the cells in the first row with the letters A-D, and then inserts a formula in the second row. Obviously, in your project, you would put the appropriate logic.
As we can see, it’s not difficult at all to connect to Microsoft Excel from a NET6 application. From there, we could perform all the actions we normally have in an Excel sheet, such as adding tabs, formulas, charts…
That’s all for today. In the next post, we will see how to do the same with Microsoft Outlook. See you next time!
Download the Code
All the code from this post is available for download on Github.

