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 i
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();
}
Microsoft.Office.Interop.Excel.Application oXL = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook oWB;
Microsoft.Office.Interop.Excel._Worksheet oSheet;
Microsoft.Office.Interop.Excel.Range oRng;
//create exel
oWB = oXL.Workbooks.Open(@"C:\Users\\Desktop\Test.xlsx");
oXL.Visible = true;//Can make it false when don't want to see the excel file
//give u name of workbook
string ExcelWorkbookname = oWB.Name;
// statement get the worksheet count
int worksheetcount = oWB.Worksheets.Count;
string path = oWB.Path;
oSheet = (_Worksheet)oWB.Sheets.get_Item(1);
string str = oSheet.Name;
//IMP: on Excel right click on drop down u can see the drop down name on left top most corner which u r providing below.
// It can be anything like Drop Down 5,Drop Down 6,Drop Down 7 etc
var dropdownValue= oSheet.Shapes.Item("Drop Down 5").ControlFormat;
dropdownValue.ListIndex = 2; //This allows you to change the drop down to 2nd element
//this code works but Interop is very very slow.Instead use Oledb
Use this to navigate through the drop down list.If you are using drop down object. We are using shapes.item
You need to identify the shape object name from your excel.
var control = xlWorksheet.Shapes.Item("Drop Down 22").ControlFormat;
control.ListIndex = 5; \\This allows you to change the drop down to 5th element
In case you are trying to change a combo box which is based on the cell of a excel sheet , you can directly change the cell value
Excel.Range xlRangeloc= xlWorksheetH.get_Range("D5");
xlRangeloc.Value = "OptionOne";