0

こんにちは、私は 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];

それとその後?私は何をすべきか?

4

1 に答える 1

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;
    }

それがあなたを助けるかどうか私に知らせてください。

于 2012-06-12T05:48:59.707 に答える