zoukankan      html  css  js  c++  java
  • 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;

            }

    有些事现在不做,一辈子都不会做了
  • 相关阅读:
    ES6基础
    RegExp正则表达式基础
    JavaScript基础之--- 手写 bind 方法的实现
    JavaScript基础之--- 手写 apply方法 的实现
    JavaScript基础之--- 手写 call 的实现
    JavaScript基础之--- 手写instanceof 的实现
    git系列之---工作中项目的常用git操作
    git系列之---将本地的项目添加到码云仓库
    git系列之---码云gitee 添加SHH公钥
    TCP的三次握手过程
  • 原文地址:https://www.cnblogs.com/mengkai/p/6187720.html
Copyright © 2011-2022 走看看