Usually the items are binded to the Excel ComboBox by specifying the range of the cells. The values present in the particular range of cells are listed as ComboBox items in Excel files. Also, Essential XlsIO returns the proper range even through the referenced/binded cells are in different worksheet. The property "xlComboBox.ListFillRange" holds the range of the cells which are referenced/binded for populating the combobox items. Using this property, you can retrieve the range and then iterate through the range to get all the combobox items. Herewith i have attached the code snippet to retrieve the ComboBox items.
private void button1_Click(object sender, EventArgs e)
{
//Instantiate the spreadsheet creation engine.
ExcelEngine excelEngine = new ExcelEngine();
//Instantiate the excel application object.
IApplication application = excelEngine.Excel;
//Open the excel file and instantiate the workbook object
IWorkbook workbook = application.Workbooks.Open(@"..\..\Data\Book1.xlsx");
//Retrieve the Excel comboBox from the worksheet
IComboBoxShape xlComboBox = workbook.Worksheets[0].ComboBoxes[0];
//user defined method to retrieve Excel ComboBox items and populate them in a Windows forms - ComboBox control
RetrieveItemsFromExcelComboBox(xlComboBox, comboBox1);
xlComboBox = workbook.Worksheets[0].ComboBoxes[1];
RetrieveItemsFromExcelComboBox(xlComboBox, comboBox2);
//Close the workbook.
workbook.Close();
//Dispose the excel engine
excelEngine.Dispose();
}
/// <summary>
/// Retrieve the items from the Excel ComboBox and populate them in Windows form - ComboBox control
/// </summary>
/// <param name="xlComboBox">Excel combobox instance (IComboBoxShape)</param>
/// <param name="comboBox">Windows Forms - Combo Box instance</param>
private void RetrieveItemsFromExcelComboBox(IComboBoxShape xlComboBox, ComboBox wfComboBox)
{
//Get the range where the ComboBox items are present in the workbook
IRange xlRange = xlComboBox.ListFillRange;
//iterate through the range of the comboBox items and add them into the Windows forms - ComboBox control
for (int rowIndex = xlRange.Row; rowIndex <= xlRange.LastRow; rowIndex++)
{
for (int colIndex = xlRange.Column; colIndex <= xlRange.LastColumn; colIndex++)
{
wfComboBox.Items.Add(xlRange[rowIndex, colIndex].DisplayText);
}
}
wfComboBox.SelectedIndex = xlComboBox.SelectedIndex - 1;
}
Let me know if it helps you.