.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); } }
More Excel tutorial
- How to supply MS Excel date time data display formatted in Winforms using .NET C#
- How to use XsExcel DLL for .NET to supply Excel numeric data display formatted in .NET
- How to use XsExcel SDK for .NET to modify Spreedsheets in C#
- XLSX inserting from SQL database in C#.NET is quite easy and quick using XsExcel Component for .NET
- How to set formula to XLS in WPF using C#.NET