更改下拉列表中的值Excel C# [英] Changing the value in a drop down list Excel C#

查看:97
本文介绍了更改下拉列表中的值Excel C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我问过如何使用c#更改excel内单元格的值,但是如果我想更改下拉列表的值怎么办.我用于工作表修改的代码如下.任何帮助表示赞赏.

so I have asked how to change the value in a cell inside of excel using c#, but what if I want to change the value of a drop down list. The code I am using for sheet modifying is below. Any help is appreciated.

 public virtual Object ActiveSheet { get; set; }

 private void button3_Click(object sender, EventArgs e)
 {
 var sheet = (Excel._Worksheet)this.ActiveSheet; 
 sheet.Cells[6, 6] = "6";
 }

我打开工作表,但是当它进入列表时,它会提示我NullReferenceException.

I get the sheet to open but when it goes to the list it gives me NullReferenceException.

推荐答案

弄清楚它花了我很长时间,请使用它!

Figured it out took me a long time please use it!!

  using Excel = Microsoft.Office.Interop.Excel;

  public virtual Object ActiveSheet { get; set; }

  private void button15_Click(object sender, EventArgs e)
    {
        //Gets ActiveSheet to Modify
        Excel.Application oXL;
        Excel.Workbook oWB;
        Excel.Worksheet oSheet;

        //Start Excel and get Application object.
        oXL = (Excel.Application)Marshal.GetActiveObject("Excel.Application"); 
        oXL.Visible = true;
        oWB = (Excel.Workbook)oXL.ActiveWorkbook; 
        oSheet = (Excel.Worksheet)oWB.ActiveSheet;

        //Generate Linear Guide Supports using Design Table in Solidworks
        if (comboBox1.Text == "0")//no external rails
        {
            oSheet.Cells[6, 4] = "0"; //Change Value in Cell in Excel Cell Location [y-axis, x-axis]
        }
        //Quit Excel
        oXL.Quit();
    }

这篇关于更改下拉列表中的值Excel C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆