更改下拉列表中的值Excel C#

所以我已经问过如何使用c#改变excel里面的单元格的值,但是如果我想改变一个下拉列表的值,该怎么办? 下面是我用于修改表单的代码。 任何帮助表示赞赏。

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。

想通了,花了很长时间请使用它!

  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(); } 

在第二行设置一个断点,并查看哪个sheet等于。

如果您尚未激活工作簿中的工作表,那么wb.ActiveSheet将不会等于任何内容。

我想你明确必须将sheet设置为wb.Sheets(0)或任何表单所需。