News Category

Get a list of the worksheet names in an Excel workbook

2017-05-23 09:12:02 Written by  support iceblue
Rate this item
(0 votes)

When we deal with a workbook with a large number of worksheets, we may need to get the names of those worksheets and then it is easy for us to find the information we want. This article will demonstrate how to use Spire.XLS to get a list of the worksheets in the workbook in two parts.

Get a list of all the worksheet names in a workbook.

using Spire.Xls;
using Spire.Xls.Collections;
using System;
namespace WorksheetName
{
    class Program
    {

        static void Main(string[] args)
        {
            const string DEMOFILE = @"Sample.xlsx";

            var results = GetAllWorksheets(DEMOFILE);

            foreach (Worksheet item in results)
            {
                Console.WriteLine(item.Name);
            }
            Console.ReadLine();
        }
        public static WorksheetsCollection GetAllWorksheets(string fileName)
        {
            //Initialize a new Workboook object
            Workbook workbook = new Workbook();

            //Load the document
            workbook.LoadFromFile(fileName);

            //Get all worksheets
            WorksheetsCollection worksheets = workbook.Worksheets;

            return worksheets;
        }
    }
}
=

Get a list of the hidden worksheet names in a workbook

static void Main(string[] args)
 {
     const string DEMOPATH = @"Sample.xlsx";
     List sheets = GetHiddenSheets(DEMOPATH);
     foreach (var sheet in sheets)
     {
         Console.WriteLine(sheet.Name);
     }
     Console.ReadLine();
 }
 public static List GetHiddenSheets(string fileName)
 {
     List returnVal = new List();

     //Initialize a new Workboook object
     Workbook workbook = new Workbook();

     //Load the document
     workbook.LoadFromFile(fileName);

     //Judge whether a worksheet is hidden
     foreach (Worksheet sheet in workbook.Worksheets)
     {
         if (sheet.Visibility == WorksheetVisibility.Hidden)
         {
             returnVal.Add(sheet);
         }
     }
     return returnVal;
 }

Additional Info

  • tutorial_title:
Last modified on Monday, 06 September 2021 02:03