In this tutorial, I'll show you how to use the EPPlus library to read an excel file in C#.NET
To play the demo, you need to download and install the EPPlus library from the Manage Nuget Packages, then you can use the following code below to read data in your excel file
// path of your excel file
string path = "C://sample.xlsx";
FileInfo fileInfo = new FileInfo(path);
ExcelPackage package = new ExcelPackage(fileInfo);
ExcelWorksheet worksheet = package.Workbook.Worksheets.FirstOrDefault();
// get number of rows and columns in the sheet
int rows = worksheet.Dimension.Rows;
int columns = worksheet.Dimension.Columns;
// using loop through the worksheet
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= columns; j++) {
var content = worksheet.Cells[i, j].Value.ToString();
//...etc
}
}
You need to get all rows and columns, then using loop to fetch data