Learn how to read Excel files in C#: https://blog.aspose.com/2021/10/10/how-to-read-excel-files-in-csharp/
Last active
December 10, 2021 13:57
-
-
Save aspose-com-gists/d37524074e56c39cf484539a49f54c1f to your computer and use it in GitHub Desktop.
Read Excel Files in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(" "); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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