How to get the names of the worksheets

Introduction

Excel worksheets have names that are used to simplify navigation and make it possible to reference data from other worksheets. When you do create a new workbook in Excel it already has with the name “Sheet1”. I’ve created three worksheets and left their default names that is shown on the screenshot below.

Retrieving the names of the worksheets

To get the names of the worksheets call GetViewInfo() method that returns an object which contains a list of pages (worksheets). Each page has its name.  The following code sample shows how to get and print out the names of the worksheets (this example can be also found in our public examples repository.)

using (Viewer viewer = new Viewer("sample.xlsx"))
{
    ViewInfoOptions viewInfoOptions = ViewInfoOptions.ForHtmlView();
    viewInfoOptions.SpreadsheetOptions = SpreadsheetOptions.ForOnePagePerSheet();

    ViewInfo viewInfo = viewer.GetViewInfo(viewInfoOptions);

    Console.WriteLine("Worksheets:");
    foreach (Page page in viewInfo.Pages)
    {
        Console.WriteLine($" - Worksheet {page.Number} name '{page.Name}'");
    }
}       

After running this sample code the names of the worksheets are printed into a console as shown on the screenshot below.

More resources

GitHub Examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online App

Along with full-featured .NET library we provide simple but powerful free Apps. You are welcome to view Word, PDF, Excel, PowerPoint documents with free to use online GroupDocs Viewer App.