0

嗨,我是 Syncfusion 产品的新手,我需要获取在我找到的 excel 文件中创建的组合框的值:

IComboBoxShape 包含 SelectedValue 和 SelectedIndex 但不是所有值。

我应该用别的东西吗

这是我的代码

var xlApp = xl.Excel;
var wkbk = xlApp.Workbooks.Open(stream); 
var sheet1 = kbk.Worksheets[0];  
var combobox = sheet1.ComboBoxes[0];

在那之后?我应该怎么办?

4

1 回答 1

1

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.

于 2012-06-12T05:48:59.707 回答