.NET Excel Control has provided two ways to load Excel document object from file and stream. This guide is showing developers how to Use XsExcel to load and read Excel spreedsheet in C# .NET Programming Project


Read XLSX and XLS workbook from local file
// Load excel workbook from local file
Workbook workBook = new Workbook("sample.xlsx");
// Get a worksheet by name
Sheet sheet = workBook.GetSheet("Sheet1");

// Get a row by rowId
Row row = sheet.GetRow(0);
// Get a cell by cellId
Cell cell = row.GetCell(0);
CellType cellType = cell.CellType;
// Show the data of Cell("A0")
if (cellType == CellType.String)
{
    Console.WriteLine(cell.StringValue);
}
else if (cellType == CellType.Numeric)
{
    Console.WriteLine(cell.NumericValue);
}
Load XLSX and XLS spreedsheet from stream
string inputFile = "sample.xlsx";
                       
using (FileStream fileStream = File.Open(inputFile, FileMode.Open, FileAccess.Read))
{
    // Load excel workbook from file stream
    Workbook workBook = new Workbook(fileStream);
    // Get a worksheet by name
    Sheet sheet = workBook.GetSheet("Sheet1");

    // Get a row by rowId
    Row row = sheet.GetRow(0);
    // Get a cell by cellId
    Cell cell = row.GetCell(0);
    // Show the data of Cell("A0")
    CellType cellType = cell.CellType;
    if (cellType == CellType.String)
    {
        Console.WriteLine(cell.StringValue);
    }
    else if (cellType == CellType.Numeric)
    {
        Console.WriteLine(cell.NumericValue);
    }
}

Notice - If you used the trial version of XsExcel SDK, can only edit, insert or get the first 300 row data!

More Excel tutorial