List the names of all worksheets in workbook in an Excel combo box in C#, VB.NET
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files. Using this library, you can list all the sheet names of a workbook in an Excel combo box.
Steps to list all the sheet names of a workbook in combo box, programmatically:
Step 1: Create a new C# console application project.
Create a new C# console application project
Step 2: Install the Syncfusion.XlsIO.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
Install NuGet package to the project
Step 3: You can fill a combo box using ListFillRange property of IComboBoxShape, by assigning the worksheet range having values to it.
C#
//Assign the values that are to be listed in combo box
comboBox.ListFillRange = worksheet.Range["A1:A10"];
VB.NET
'Assign the values that are to be listed in combo box
comboBox.ListFillRange = worksheet.Range("A1:A10")
Step 4: Include the following namespaces in Program.cs file.
C#
using Syncfusion.XlsIO;
VB.NET
Imports Syncfusion.XlsIO
Step 5: Include the following code snippet in main method of Program.cs file to list the names of all worksheets of a workbook in Excel combo box.
C#
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Initialize the Excel application object
IApplication application = excelEngine.Excel;
//Create a new Excel workbook with 10 worksheets
IWorkbook workbook = application.Workbooks.Create(10);
//Get the first worksheet in workbook into IWorksheet
IWorksheet worksheet = workbook.Worksheets[0];
//Get the names of all sheets in workbook and assign those names in a range of first worksheet
for (int i = 0; i < workbook.Worksheets.Count; i++)
{
worksheet.Range[i + 1, 1].Text = workbook.Worksheets[i].Name;
}
//Add a combo box in the worksheet
IComboBoxShape comboBox = worksheet.ComboBoxes.AddComboBox(4, 4, 30, 100);
//Assign the values that are to be listed in combo box
comboBox.ListFillRange = worksheet.Range["A1:A10"];
//Save the Excel document
workbook.SaveAs("Output.xlsx");
}
VB.NET
Using excelEngine As ExcelEngine = New ExcelEngine()
'Initialize the Excel application object
Dim application As IApplication = excelEngine.Excel
'Create a new Excel workbook with 10 worksheets
Dim workbook As IWorkbook = application.Workbooks.Create(10)
'Get the first worksheet in workbook into IWorksheet
Dim worksheet As IWorksheet = workbook.Worksheets(0)
'Get the names of all sheets in workbook and assign those names in a range of first worksheet
For i As Integer = 0 To workbook.Worksheets.Count - 1 Step 1
worksheet.Range((i + 1), 1).Text = workbook.Worksheets(i).Name
Next
'Add a combo box in the worksheet
Dim comboBox As IComboBoxShape = worksheet.ComboBoxes.AddComboBox(4, 4, 30, 100)
'Assign the values that are to be listed in combo box
comboBox.ListFillRange = worksheet.Range("A1:A10")
'Save the Excel document
workbook.SaveAs("Output.xlsx")
End Using
A complete working sample of how to list the names of all worksheets of a workbook in an Excel combo box can be downloaded from ListOfSheetNames.zip.
By executing the program, you will get the output Excel file as follows.
Output Excel document
Other properties available under IComboBoxShape are Display3DShading, DropDownLines, LinkedCell, SelectedValue, and SelectedIndex.
Take a moment to peruse the documentation, where you will find other options like form controls, comments, auto shapes, group shapes, and OLE objects will code samples.
Click here to explore the rich set of Syncfusion Excel (XlsIO) library features.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.