C#
Excel
Data Reading
Programming
Code Example

How to read data from excel file using c

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Reading Excel data in C# is less about syntax and more about choosing the right library for the file type and deployment environment. For most modern .xlsx files, a library such as EPPlus, ClosedXML, or ExcelDataReader is simpler and safer than Office COM automation. The general rule is: use a pure .NET library unless you specifically need Excel itself installed and automated on a desktop machine.

A Good Default: EPPlus for .xlsx

EPPlus is a common choice for reading modern Excel files because the API is straightforward.

csharp
1using OfficeOpenXml;
2using System;
3using System.IO;
4
5ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
6
7var file = new FileInfo("sample.xlsx");
8
9using var package = new ExcelPackage(file);
10var worksheet = package.Workbook.Worksheets[0];
11
12for (int row = 1; row <= worksheet.Dimension.End.Row; row++)
13{
14    string name = worksheet.Cells[row, 1].Text;
15    string email = worksheet.Cells[row, 2].Text;
16    Console.WriteLine($"{name} - {email}");
17}

This is a strong option when you control the file format and are working with .xlsx.

Read with Headers Explicitly

Most real sheets have a header row that should not be treated as data.

csharp
1using OfficeOpenXml;
2using System;
3using System.IO;
4
5ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
6
7using var package = new ExcelPackage(new FileInfo("people.xlsx"));
8var sheet = package.Workbook.Worksheets[0];
9
10for (int row = 2; row <= sheet.Dimension.End.Row; row++)
11{
12    var id = sheet.Cells[row, 1].Text;
13    var name = sheet.Cells[row, 2].Text;
14    Console.WriteLine($"{id}: {name}");
15}

Starting at row 2 is a common and intentional choice when row 1 contains column names.

Alternative: ExcelDataReader for Reading Pipelines

If you mainly need to read Excel into a tabular structure, ExcelDataReader is another strong option.

csharp
1using ExcelDataReader;
2using System;
3using System.Data;
4using System.IO;
5using System.Text;
6
7Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
8
9using var stream = File.Open("sample.xlsx", FileMode.Open, FileAccess.Read);
10using var reader = ExcelReaderFactory.CreateReader(stream);
11
12var result = reader.AsDataSet();
13var table = result.Tables[0];
14
15foreach (DataRow row in table.Rows)
16{
17    Console.WriteLine($"{row[0]} - {row[1]}");
18}

This is especially useful when you want a DataTable-like result for downstream processing.

.xls Versus .xlsx

The file extension matters. Older .xls files and newer .xlsx files are different formats. Some libraries support both, some focus on .xlsx.

Before choosing a library, answer:

  • do you need .xls
  • do you need .xlsx
  • do you need formulas or just displayed values
  • do you need write support or only read support

That choice affects the right package more than the raw code syntax does.

Use Cell Text Carefully

Reading .Text is convenient because it gives user-visible formatted text. But sometimes you need the underlying typed value instead.

With EPPlus:

csharp
var rawValue = sheet.Cells[row, 3].Value;
Console.WriteLine(rawValue);

Use .Text for display-oriented ingestion and .Value when types matter.

For example, dates and numeric fields often need special handling if you plan to compute with them later.

Convert to Domain Objects

Reading raw cells is only the first step. It is usually better to map rows into typed objects.

csharp
public record Person(string Name, string Email);
csharp
1var people = new List<Person>();
2
3for (int row = 2; row <= sheet.Dimension.End.Row; row++)
4{
5    var name = sheet.Cells[row, 1].Text;
6    var email = sheet.Cells[row, 2].Text;
7    people.Add(new Person(name, email));
8}

This makes validation and downstream logic much cleaner than passing string arrays around everywhere.

Avoid COM Automation on Servers

Some older examples use Microsoft.Office.Interop.Excel. That automates a real Excel installation. It can be acceptable on a controlled desktop machine, but it is a poor choice for server-side or unattended code.

Why avoid it for typical app code:

  • requires Excel to be installed
  • harder deployment story
  • fragile in unattended environments
  • slower and more operationally complex

For most backend or utility applications, a pure .NET library is the better path.

Handle Empty Cells and Bad Data

Excel files are often messy. Expect blank cells, mixed types, and user formatting surprises.

csharp
1var email = sheet.Cells[row, 2].Text?.Trim();
2if (string.IsNullOrWhiteSpace(email))
3{
4    continue;
5}

Treat Excel as external input, not trusted structured data.

Common Pitfalls

The biggest mistake is choosing a library before checking whether the file is .xls or .xlsx. Another is reading formatted text when the business logic actually needs typed numeric or date values. Developers also often reach for Excel COM automation when a pure .NET library would be simpler and safer. Finally, Excel input should always be validated because blank cells, merged cells, and inconsistent user formatting are common.

Summary

  • Use a pure .NET library such as EPPlus or ExcelDataReader for most Excel-reading tasks in C#.
  • Choose the library based on file format and whether you need read-only or richer workbook support.
  • Skip header rows intentionally when they are not data.
  • Distinguish between displayed text and raw cell values.
  • Avoid Office COM automation unless you specifically need desktop Excel integration.

Course illustration
Course illustration

All Rights Reserved.