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;

            }

    有些事现在不做,一辈子都不会做了
  • 相关阅读:
    查看Eclipse的版本
    eclipse3.7.1加载swt失败(转载)
    [转载]android的常用开发包
    JSP 两种注释的区别,代码段
    Delphi TAdvStringGrid 在表格显示出下拉列表
    Delphi TAdvStringGrid的回车换格功能
    Delphi TAdvStringGrid表格录入汉字总是给省略两个字的原因。
    Mac 终端命令大全
    Mac OS 下三种修改Hosts文件的方法
    Memcached与Memcache区别
  • 原文地址:https://www.cnblogs.com/mengkai/p/6187720.html
Copyright © 2011-2022 走看看