In Excel, the worksheet names can serve as a form of metadata about the workbook's contents. By retrieving a list of these names, it is possible to have a rough understanding of what each worksheet is used for and provide an overview of where a certain type of data is stored. This is particularly useful for larger workbooks or when collaborating with others. In this article, you will learn how to get sheet names in Excel in C# using Spire.XLS for .NET.
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Get All Worksheet Names in Excel in C#
The Worksheet.Name property returns the name of a Worksheet. To retrieve the names of all the worksheets in Excel (including hidden ones), you can iterate through each worksheet and use this property to get their names. The detailed steps are as follows:
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get all the worksheets in the Excel workbook through Workbook.Worksheets property.
- Iterate through each worksheet.
- Get the name of each worksheet through Worksheet.Name property and then output the results.
- C#
using Spire.Xls; using Spire.Xls.Collections; namespace WorksheetName { class Program { static void Main(string[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel document workbook.LoadFromFile("Budget.xlsx"); // Get all the worksheets in Excel WorksheetsCollection worksheets = workbook.Worksheets; // Iterate through each worksheet foreach (Worksheet sheet in worksheets) { // Get worksheet name Console.WriteLine(sheet.Name); } } } }
Get Hidden Worksheet Names in Excel in C#
If you only need to retrieve the names of the hidden worksheets, you can first iterate through each worksheet to determine whether a worksheet is hidden, and if so, get its name through the Worksheet.Name property. The detailed steps are as follows:
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get all the worksheets in the Excel workbook through Workbook.Worksheets property.
- Iterate through each worksheet and find the hidden worksheets.
- Get the names of the hidden worksheets through Worksheet.Name property and then output the results.
- C#
using Spire.Xls; using Spire.Xls.Collections; namespace HiddenSheetsName { class Program { static void Main(string[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel document workbook.LoadFromFile("E:\\PythonExcel\\Monthly company budget.xlsx"); // Get all the worksheets in Excel WorksheetsCollection worksheets = workbook.Worksheets; // Iterate through each worksheet foreach (Worksheet sheet in worksheets) { // Detect the hidden worksheet if (sheet.Visibility == WorksheetVisibility.Hidden) { // Get the hidden worksheet name Console.WriteLine(sheet.Name); } } } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.