How to get the names of the worksheets

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

View Excel Files Online

Along with full-featured .NET library we provide simple but powerful free online Apps. View Excel XLS, XLSX, and XLSB files online with GroupDocs Viewer App.

GitHub Examples

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