Read Excel File With C# (INTEROP)

Automation of an Excel file allows us to doing various operations from C#. We can automate an Excel file from C# in two ways. Using Excel Object Model (using Microsoft.Office.Interop.Excel) in one way and another way is using Microsoft Jet Engine (OLEDB) to connect Excel from CSharp.

Using Excel Object Model method to connect and read excel is explained in this article. You can also see article “READ EXCEL FILE WITH C# (OLEDB)” to read Microsoft Jet Engine (OLEDB) method to connect and read excel.

First we are creating an excel file to use in this article. Filename is “classroom.xlsx” (office 2010 file) and first worksheet named as “class_list”. We are filling the worksheet as follows;

In MS Visual Studio (used 2010), open a new Windows Form Application. By using textbox(txtFileDestination), button(btnBrowse), label(lblDataToShow) and openFileDialog controls create a form as fallows;

Then from solution explorer, right click “References” and add reference “Microsoft.Office.Interop.Excel” with appropriate version number (for Office 2010, version 14.0.0.0 and for Office 2007 version 12.0.0.0).

Double click the browse button and start coding. Codes are as fallows,