How to Get Excel File Sheet Name List Using C#, Console Windows Application

How to Get Excel File Sheet Name List Using C#, Console Windows Application. Here we will use closexml to read the sheet name list and print.
In this article I will show you how you can get the list of all the sheet available in an excel file using c#.net in windows application, console application, and mvc application. Here I am using ClosedXML to read the excel file and get the sheet name list. 

My previous article is Asp.Net Core Tutorial: Barcode Generation with C#.Net.

Few of my previous article which you may like.

Let's create and excel file and add some sheet in it. 

Excel sheet

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.

Excel Sheet List in c#
Here you can see we are getting two of the sheet names at code end. Now lets print the final output.

How to Get Excel File Sheet Name List Using C#, Console  Windows Application

Post a Comment