您好我是Syncfusion产品的新手, 我需要获取在excel文件中创建的组合框的值 我找到了:
包含SelectedValue和SelectedIndex的IComboBoxShape 但不是所有的价值观。
我应该使用其他东西
这是我的代码
var xlApp = xl.Excel;
var wkbk = xlApp.Workbooks.Open(stream);
var sheet1 = kbk.Worksheets[0];
var combobox = sheet1.ComboBoxes[0];
之后呢?我该怎么办?
答案 0 :(得分:1)
通常,通过指定单元格的范围将项目绑定到Excel ComboBox。特定单元格范围中的值在Excel文件中列为ComboBox项。此外,即使引用/绑定单元格位于不同的工作表中,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;
}
让我知道它是否对你有所帮助。