Here are the codes:
Excel.Application xlsApp = new Excel.ApplicationClass();
Excel.Workbook xlsWorkbook;
Excel.Worksheet xlsWorksheet;
object oMissing = System.Reflection.Missing.Value;
//Create new workbook
xlsWorkbook = xlsApp.Workbooks.Add(true);
//Get the first worksheet
xlsWorksheet = (Excel.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");
Excel.DropDowns xlDropDowns;
Excel.DropDown xlDropDown;
xlDropDowns = ((Excel.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;
Cheers!
21 comments:
Fantastic code. thanks so much!
How to select the value in the Excel.dropdown?
How to read the DropDown Selected value back? any help?
I've only found how to get the index of the selected value; I can't seem to get the text.
Excel.DropDown xlDropDown = (Excel.DropDown)sheet.DropDowns("Drop Down 4");
int index = xlDropDown.ListIndex;
The artical is fantastic. I need some more additionas to the drop down. I want to add the drop down to one particular column and repeat the dropdown through out the sheet. Also, i would like to upload the excel and read the selected dropdown from c# code. Thank you in advance.
The artical is fantastic. I need some more additionas to the drop down. I want to add the drop down to one particular column and repeat the dropdown through out the sheet. Also, i would like to upload the excel and read the selected dropdown from c# code. Thank you in advance.
Thanks for the post. After writing everything i couldn't find out any excel file. How can i open the excel file with this code on fly .
Thanks in advance...Please urgent
Hi Satya, you can open an existing Excel file by using Open method, as below:
xlWorkBook = xlApp.Workbooks.Open("yourexcelfile.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
Thank You july,
I have another question.How can i select the dropdown values from range of cell(particualr column).The values are able to see in dropdown, but cant select those values.Can u please lemme know how to select those value.
thanks in advance
Satya
Satya,
Perhaps dd.Text will do (dd is the drop down in your excel file).
Thank you so much. can you send more about managing excel sheet
thanks
Pradeep
Buildium
Thanks for the code, i have added 2 dropdowns and the values are one having country name and other having states. when user selects any country the next dropdown should get filtered by that selected country's states. please help.
Here is the code to get the selected value from the Dropdown
for (int j = 1; j < 100; j++)
{
Excel.DropDown xlDropDown1 = (Excel.DropDown)ObjWs.DropDowns("Drop Down "+j.ToString());
string selectedText = xlDropDown1.get_List(xlDropDown1.ListIndex).ToString();
}
--By Jyoti
did the DropDown(s) go away with the newest version of Office? I don't have an Excel.DropDown(s) option available to me..
thanks
hi votary, which version of excel dll you are using? i think is because of the excel version.
Hi..
Can u plz explain me how to use
Excel.DropDown.OnAction
Really it's very usefull..Nice post
Nice code, really its helpful..
HI g,
I used your coding.but i got this
Exception
Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Excel.Range'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00020846-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
Can u tel how to resolve this one...?
visual studio 2005
allaudhin.be@gmail.com
Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Excel.Range'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00020846-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
Can you please mention on which line are you getting the exception?
Post a Comment