How to Open and Read From Excel File in C#

How to Open and Read From Excel File in C#

To open and read data from an Excel file in C#, you typically use a library since .NET doesn’t natively handle Excel formats like .xlsx. The most common options are EPPlus, NPOI, and ClosedXML. Here’s a clear, practical way using EPPlus (one of the simplest).

1. Install EPPlus

Use NuGet Package Manager:

Install-Package EPPlus

Basic Example: Read Excel File

using System;
using System.IO;
using OfficeOpenXml;

class Program
{
    static void Main()
    {
        var filePath = @"C:\path\to\your\file.xlsx";

        // EPPlus requires setting the license context
        ExcelPackage.LicenseContext = LicenseContext.NonCommercial;

        using (var package = new ExcelPackage(new FileInfo(filePath)))
        {
            var worksheet = package.Workbook.Worksheets[0]; // First sheet

            int rowCount = worksheet.Dimension.Rows;
            int colCount = worksheet.Dimension.Columns;

            for (int row = 1; row <= rowCount; row++)
            {
                for (int col = 1; col <= colCount; col++)
                {
                    var value = worksheet.Cells[row, col].Text;
                    Console.Write(value + "\t");
                }
                Console.WriteLine();
            }
        }
    }
}

Key Points of EPPlus

• Excel rows and columns are 1-based (not 0-based).
• worksheet.Cells[row, col].Text gives formatted text.
• Use .Value if you want raw data instead of formatted strings.
• Always wrap file access in using to release resources.

Alternative: ClosedXML (Very Readable)

Install:

Install-Package ClosedXML

ClosedXML Example:

using System;
using ClosedXML.Excel;

class Program
{
    static void Main()
    {
        var filePath = @"C:\path\to\your\file.xlsx";

        using (var workbook = new XLWorkbook(filePath))
        {
            var worksheet = workbook.Worksheet(1);

            foreach (var row in worksheet.RowsUsed())
            {
                foreach (var cell in row.Cells())
                {
                    Console.Write(cell.Value.ToString() + "\t");
                }
                Console.WriteLine();
            }
        }
    }
}

When to Use What?

• EPPlus → Fast, flexible, widely used
• ClosedXML → Cleaner syntax, easier to read/write
• NPOI → Good for legacy .xls (older Excel files)

Alternatively use Microsoft.Office.Interop.Excel

First step is to add necessary references to the project.

1. Right click References and press Add References item in the menu.
2. Go to COM Tab and select Type Libraries.
3. Select the ticks of
    - Microsoft Excel 14.0 Object Library
    - Microsoft Office 14.0 Object Library

Required using statements:

using Microsoft.Office.Interop.Excel;

Here is the example code:

public void ReadExcelFile(String fileName)
{
    var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);

    var app = new Application();
    var workbook = app.Workbooks.Open(path);

    if (workbook.Sheets.Count > 0)
    {
        var worksheet = workbook.Sheets[1];
        var range = worksheet.UsedRange;

        var numRows = range.Rows.Count;
        var numCols = range.Columns.Count;

        for (int j = 1; j < numRows; ++j)
            for (int i = 1; i < numCols; ++i)
                if (range.Cells[j, i].Value2 != null)
                    var data = (Double.Parse(range.Cells[j, i].Value2.ToString()));

        workbook.Close(true, null, null);
        app.Quit();

        ReleaseObject(worksheet);
        ReleaseObject(workbook);
        ReleaseObject(app);
    }
}

private void ReleaseObject(object obj)
{
    try
    {
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
        obj = null;
    }
    catch (Exception ex)
    {
        obj = null;
        Console.WriteLine("Unable to release the Object {0}", ex.ToString());
    }
    finally
    {
        GC.Collect();
    }
}

Contents related to 'How to Open and Read From Excel File in C#'

C# File Operations, C# Input Output (C# I/O)
C# File Operations, C# Input Output (C# I/O)
How to Create and Write Excel File in C#
How to Create and Write Excel File in C#
How to create xml document in C#
How to create xml document in C#
Open a text file and read line by line in C#
Open a text file and read line by line in C#