Syncfusion XlsIO中的组合框
本文关键字:组合 XlsIO Syncfusion | 更新日期: 2023-09-27 18:20:19
嗨,我是Syncfusion产品的新手,我需要获取在excel文件中创建的组合框的值我发现:
包含SelectedValue和SelectedIndex的组合框形状但并不是所有的价值观。
我应该用别的东西吗
这是我的代码
var xlApp = xl.Excel;
var wkbk = xlApp.Workbooks.Open(stream);
var sheet1 = kbk.Worksheets[0];
var combobox = sheet1.ComboBoxes[0];
之后呢?我该怎么办?
通常通过指定单元格的范围将项绑定到Excel组合框。特定单元格范围中的值在Excel文件中列为组合框项。此外,即使引用/绑定的单元格位于不同的工作表中,Essential XlsIO也会返回正确的范围。属性"xlComboBox.ListFillRange"包含为填充组合框项而引用/绑定的单元格的范围。使用此属性,您可以检索该范围,然后在该范围中迭代以获得所有组合框项。在此,我附上了检索ComboBox项的代码片段。
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;
}
如果对你有帮助,请告诉我。