My previous article is Asp.Net Core Tutorial: Barcode Generation with C#.Net.
Few of my previous article which you may like.
- ASP.NET Core Tutorial: Generate QR Codes with C#.NET
- Read Excel File Data into DataSet C#, .Net Core
- Step‑by‑Step Guide: Import and Display Excel Data in ASP.NET Core MVC with C#
- Upload, Read and Display Excel File in .Net Core Using C#.net Windows Application
Let's create and excel file and add some sheet in it.
Now for this I will use a simple console application to read the sheet name and return the List<string> of sheet name. After creating the file we will install the NuGet Package ClosedXML.
class Program
{
static void Main()
{
string filePath = @"C:\path\file.xlsx";
List<string> sheetNames = GetExcelSheetNames(filePath);
foreach (string sheet in sheetNames)
{
Console.WriteLine(sheet);
}
}
private static List<string> GetExcelSheetNames(string filePath)
{
var sheets = new List<string>();
using (var workbook = new XLWorkbook(filePath))
{
foreach (var worksheet in workbook.Worksheets)
{
sheets.Add(worksheet.Name);
}
}
return sheets;
}
} Here in above code I have create a method named as GetExcelSheetNames. Which is accepting the which file path if input excel file. i have used XLWorkbook to all the workbook from the file and stored in in the List of string. You can use it in a class library and user it commenly in your project. Now lets run the code to check the output. Now lets place the break point and check the output.
Here you can see we are getting two of the sheet names at code end. Now lets print the final output.


