Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active December 10, 2021 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/d37524074e56c39cf484539a49f54c1f to your computer and use it in GitHub Desktop.
Save aspose-com-gists/d37524074e56c39cf484539a49f54c1f to your computer and use it in GitHub Desktop.
Read Excel Files in C#
// Load Excel file
Workbook wb = new Workbook("excel.xlsx");
// Get all worksheets
WorksheetCollection collection = wb.Worksheets;
// Loop through all the worksheets
for (int worksheetIndex = 0; worksheetIndex < collection.Count; worksheetIndex++)
{
// Get worksheet using its index
Worksheet worksheet = collection[worksheetIndex];
// Print worksheet name
Console.WriteLine("Worksheet: " + worksheet.Name);
// Get number of rows and columns
int rows = worksheet.Cells.MaxDataRow;
int cols = worksheet.Cells.MaxDataColumn;
// Loop through rows
for (int i = 0; i < rows; i++)
{
// Loop through each column in selected row
for (int j = 0; j < cols; j++)
{
// Pring cell value
Console.Write(worksheet.Cells[i, j].Value + " | ");
}
// Print line break
Console.WriteLine(" ");
}
}
// Load Excel file
Workbook wb = new Workbook("excel.xlsx");
// Get worksheet using its index
Worksheet worksheet = wb.Worksheets[0];
// Print worksheet name
Console.WriteLine("Worksheet: " + worksheet.Name);
// Get number of rows and columns
int rows = worksheet.Cells.MaxDataRow;
int cols = worksheet.Cells.MaxDataColumn;
// Loop through rows
for (int i = 0; i < rows; i++)
{
// Loop through each column in selected row
for (int j = 0; j < cols; j++)
{
// Pring cell value
Console.Write(worksheet.Cells[i, j].Value + " | ");
}
// Print line break
Console.WriteLine(" ");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment