How to open an Excel file in C#

To open an Excel file in C#, developers can employ the Excel Interop library, which provides a means to interact with Excel files using C#.

To open or read an Excel file in C#, the first step is to incorporate the Microsoft Excel 12.0 Object Library into the project. By adding this library, developers gain access to the necessary functionalities and classes required for seamless interaction with Excel files. This enables the program to effectively open and read the contents of an Excel file, facilitating data retrieval and manipulation as needed.

Create a new project and add a Command Button to the Form.

Form the following images you can find how to add Excel reference library in your project.

Select Reference Dialogue from Project menu.

scsharp-excel-reference

Select Microsoft Excel 12.0 Object Library and click OK button.

csharp-excel-library

Open an Excel file

Here's an example of how you can open an Excel file:

First, make sure you have added a reference to the Microsoft.Office.Interop.Excel library in your project.

Import the necessary namespaces in your C# code file:

using Excel = Microsoft.Office.Interop.Excel;

Within your code, create an instance of the Excel Application object:

Excel.Application excelApp = new Excel.Application();

Specify the file path of the Excel file you want to open:

string filePath = "C:\\Path\\to\\YourExcelFile.xlsx";

Open the Excel file using the Application object:

Excel.Workbook workbook = excelApp.Workbooks.Open(filePath);

Once the file is opened, you can access and manipulate its worksheets, data, and perform various operations as needed. For example, to read data from a specific worksheet, you can use the following code:

Full Source C#
using System.Windows.Forms; using Excel = Microsoft.Office.Interop.Excel; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Excel.Application xlApp ; Excel.Workbook xlWorkBook ; Excel.Worksheet xlWorkSheet ; object misValue = System.Reflection.Missing.Value; xlApp = new Excel.Application(); xlWorkBook = xlApp.Workbooks.Open("csharp.net-informations.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); MessageBox.Show(xlWorkSheet.get_Range("A1","A1").Value2.ToString()); xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp); } private void releaseObject(object obj) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null; } catch (Exception ex) { obj = null; MessageBox.Show("Unable to release the Object " + ex.ToString()); } finally { GC.Collect(); } } } }

Note: It's important to handle exceptions and perform error checking when working with Excel files in C# to ensure proper handling of any unexpected situations.

Conclusion

By following these steps, you can open an Excel file in C# and begin performing operations on the data within it.