Syncfusion XlsIO 中的组合框

发布于 2024-12-27 02:11:38 字数 340 浏览 1 评论 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];

使用另一件事吗?我应该怎么办?

Hi I am new with Syncfusion product,
I need to get the values of combobox created in excel file
I found :

IComboBoxShape that contains SelectedValue and SelectedIndex
But not all the values.

Should I use another thing

here is my code

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

and after that? What should I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怂人 2025-01-03 02:11:38

通常,通过指定单元格范围将项目绑定到 Excel 组合框。特定单元格范围中存在的值在 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;
    }

如果对您有帮助,请告诉我。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文