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.

    try (Viewer viewer = new Viewer("sample.xlsx")) {
        ViewInfoOptions viewInfoOptions = ViewInfoOptions.forHtmlView();
        ViewInfo viewInfo = viewer.getViewInfo(viewInfoOptions);
        for (Page page : viewInfo.getPages()) {
            System.out.println(" - Worksheet " + page.getNumber() + " name '" + page.getName() + "'");

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 Java 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.