我已经阅读了很多关于这个主题的主题。我尝试了很多变化,但由于某种原因,我无法让它工作。这是问题所在:我有一个组合框,其中填充了 Excel 工作簿中的工作表名称。(我将包含该代码,以防出现问题)当我尝试获取选定的文本值时,我什么也得不到。我究竟做错了什么?
/********************************************************************************
* The following code takes in an excel filename and populates a combobox based
* on the excel tab names.
********************************************************************************/
private void GetExcelSheetNames(string excelFile)
{
_Application xlApp;
Workbook xlTemplateWB;
xlApp = new ApplicationClass();
xlTemplateWB = xlApp.Workbooks.Open(Elmnt.getDBPath() + TEMPLATENAME, 0, true,
5, "", "", false, XlPlatform.xlWindows, "", true, false, 0, true, false, false);
foreach (Worksheet temp in xlTemplateWB.Worksheets)
{
cboBenchSheets.Items.Add(temp.Name);
}
xlTemplateWB.Close(true, Missing.Value, Missing.Value);
xlApp.Quit();
}
/********************************************************************************
* This returns the selected item in the cboBenchSheets combo box
********************************************************************************/
public String getSelection()
{
String selected;
selected = cboBenchSheets.Text; //Returns nothing
selected = cboBenchSheets.SelectedText; //Returns nothing
selected = cboBenchSheets.SelectedValue.ToString(); //Returns nothing
selected = cboBenchSheets.GetItemText(cboBenchSheets.SelectedIndex); //Returns -1
return selected;
}