Read data in Excel files using C#

Excel spreadsheets are a common format for storing and organizing data, making them a valuable resource for businesses and individuals. Whether you need to analyze financial data, generate reports, or manipulate datasets, working with Excel files in your C# applications can be a critical requirement. Aspose.Cells for .NET is a powerful library that simplifies Excel file manipulation in C#. In this blog post, we’ll explore how to read Excel files using C# in a .NET application, offering a step-by-step guide to help you get started.

C# API to Read Excel Files

To read data from the Excel sheets, we will use Aspose.Cells for .NET. It is a powerful and feature-rich API to create and manipulate Excel files. Moreover, it allows you to read Excel files quite easily. The API can be downloaded from the downloads section or installed via NuGet.

PM> Install-Package Aspose.Cells

Read Data from an Excel File in C#

In Excel files, the data is stored inside cells where each cell is identified by its name (A1, B3, etc.) or the row and column index. The collection of cells makes a worksheet and an Excel file may contain single or multiple worksheets. Finally, all the worksheets collectively form a workbook. Aspose.Cells for .NET uses the same naming conventions to manipulate Excel files.

The following are the steps to read an Excel file and get data in C#.

  • Load the Excel file using Workbook class.
  • Create an object of WorksheetCollection class and get the reference of the worksheets using Workbook.Worksheets.
  • Start a loop to go through all the worksheets in the collection and in each iteration, perform the following steps:
    • Get a reference of the worksheet in a Worksheet object.
    • Get a count of data rows and columns in the worksheet.
    • Start a loop for rows.
    • Start a nested loop for columns.
    • Read data from each cell using Worksheet.Cells[i, j].Value property.

The following code sample shows how to read an Excel file in C#.

The following is the output that we get in the console after running the code sample above.

Reading data in the worksheets of Excel file in C#

Reading an Excel File in C#

Read a Specific Excel Sheet

You can also read only a particular worksheet in the Excel file by following the steps below.

The following code sample shows how to read data from a particular worksheet in C#.

Get Free API to Read Excel Data

You can use Aspose.Cells for .NET and read Excel files without evaluation limitations by getting a free temporary license.

Conclusion

Reading Excel files in C# using Aspose.Cells for .NET is a straightforward and efficient process. Aspose.Cells provides a comprehensive set of features to handle all aspects of Excel file manipulation in your .NET applications. Whether you need to extract data, format sheets, or perform more advanced tasks, Aspose.Cells simplifies the process and helps you achieve your Excel-related goals with ease.

You can explore other features of Aspose.Cells for .NET using the documentation. In case you would have any queries, feel free to let us know via our forum.

See Also