简体   繁体   English

如何使用oledb在C#中将下拉列数据插入到Excel中

[英]how to insert dropdown column data into excel in c# using oledb

I want to insert data into excel. 我想将数据插入excel。 But, excel contains dropdown columns and normal columns. 但是,excel包含下拉列和普通列。 I am using oledb provider to insert the data into excel normal columns but i can't insert dropdown column values using oledb, could someone point me in the right direction please? 我正在使用oledb提供程序将数据插入到excel普通列中,但是我无法使用oledb插入下拉列值,有人可以向我指出正确的方向吗?

By using Excel Interop you can achieve the drop down list. 通过使用Excel Interop,可以实现下拉列表。

Below is the example code 下面是示例代码

        // Create an Excel object
        Microsoft.Office.Interop.Excel.Application excel = new  Microsoft.Office.Interop.Excel.Application();

        //Create workbook object
        string str = @"E:\test.xlsx";
        Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Open(Filename: str);
        Microsoft.Office.Interop.Excel.Worksheet worksheet1 = workbook.ActiveSheet;
        Microsoft.Office.Interop.Excel.Range range = worksheet1.get_Range("A1","A1");
        Microsoft.Office.Interop.Excel.DropDowns xlDropDowns;
        Microsoft.Office.Interop.Excel.DropDown xlDropDown;
        xlDropDowns = ((Microsoft.Office.Interop.Excel.DropDowns)(workbook.ActiveSheet.DropDowns(Type.Missing)));
        xlDropDown = xlDropDowns.Add((double)range.Left, (double)range.Top, (double)range.Width, (double)range.Height, true);
        xlDropDown.AddItem("item1",1);
        xlDropDown.AddItem("item2", 2);


        //Save the workbook
        workbook.Save();

        //Close the Workbook
        workbook.Close();

        // Finally Quit the Application
        ((Microsoft.Office.Interop.Excel._Application)excel).Quit();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM