Add a Drop Down List into Excel File using C# .NET

Add a Drop Down List into Excel File using C# .NET

Anuradha  6:54 AM  C# No comments :

Here is sample code to add a drop down list into an Excel File using C# .NET.

Before you adding this codes, make sure you have add the Microsoft Excel Object Library from COM as reference into your project.

Here are the codes:

using Microsoft.Office.Interop.Excel ;

  protected void Button1_Click(object sender, EventArgs e)

        {

            string Filename = "samp.xls";

            Application xlsApp = new Application();

            Workbook xlsWorkbook;

            Worksheet xlsWorksheet;

            object oMissing = System.Reflection.Missing.Value;

            //Create new workbook

            xlsWorkbook = xlsApp.Workbooks.Add(true);

            //Get the first worksheet

            xlsWorksheet = (Worksheet)(xlsWorkbook.Worksheets[1]);

            string[] ddl_item = { "Answers", "Autos", "Finance", "Games", "Groups", "HotJobs",

"Maps", "Mobile Web", "Movies", "Music", "Personals", "Real Estate", "Shopping", "Sports",

"Tech", "Travel", "TV", "Yellow Pages" };

            Range xlsRange;

            xlsRange = xlsWorksheet.get_Range("A1", "A1");

            DropDowns xlDropDowns;

            DropDown xlDropDown;

            xlDropDowns = ((DropDowns)(xlsWorksheet.DropDowns(oMissing)));

            xlDropDown = xlDropDowns.Add((double)xlsRange.Left, (double)xlsRange.Top, (double)xlsRange.Width, (double)xlsRange.Height, true);

            //Add item into drop down list

            for (int i = 0; i < ddl_item.Length; i++)

            {

                xlDropDown.AddItem(ddl_item[i], i + 1);

            }

            xlsApp.DisplayAlerts = false;

            xlsWorkbook.Close(true, Filename, null);

            xlsApp.Quit();

            xlsWorksheet = null;

            xlsWorkbook = null;

            xlsApp = null;

        }

posted @ 2016-12-16 17:13 画个逗号给明天 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值