This article explains how to read Excel file in C#. It includes the IDE configuration details and stepwise process followed by a working sample code to create an XLSX reader in C#. Moreover, it covers different options to improvise the data extraction process based on your requirements.
Steps to Read Excel in C#
- Prepare the environment to use Aspose.Cells for .NET to read Excel spreadsheets
- Load the input file and get the sheet with the Workbook.Worksheets property
- Get the number of rows and columns that contain data
- Set the loops to iterate the rows and their respective columns
- Read all the cells from the Excel file with the Cell.Value property
The steps above elaborate on how to read Excel in C#. Initiate the process by loading the source Excel spreadsheet, then access the target worksheet to retrieve the data. Subsequently, iterate through each cell in the data range and print its value.
Code to Create XLSX Reader in C#
This basic code snippet illustrates creating an XLSX file reader in C#. Firstly, the Workbook class is used to load the input file, and then a collection named WorksheetCollection is used to retrieve a specific worksheet using its index number. You can access any worksheet using its name, or you can also customize this code to iterate through each worksheet in the Excel file when you need to extract all the data in the file.
This article has discussed the details to read XLSX in C#. To learn about freezing panes, read the article on Freeze Panes in Excel using C#.