Hi
I'm trying to export some data into an excel sheet (office version 2010). I want do this with Reflection. I've tried following:
Type excelType = Type.GetTypeFromProgID("Excel.Application");
object objExcel = Activator.CreateInstance(excelType);
object objBooks = objExcel.GetType().GetProperty("Workbooks").GetValue(objExcel, null);
object objBook = objBooks.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, objBooks, new[] { Missing.Value });
object objSheet = objBook.GetType().GetProperty("Worksheets").GetValue(objBook, null);
At the last line the code crashs cause objBook.GetType().GetProperty("Worksheets") returns null.
What is wrong here?
Regards
Loading
VulpesPosted May 21, 2012, 10:56 AM
To obtain the first WorkSheet, try it like this:
Pascal BeerPosted May 21, 2012, 12:43 PM
Have a nice day
Pascal
Pascal BeerPosted May 21, 2012, 10:05 AM
Jignesh TrivediPosted May 21, 2012, 9:17 AM
I thing objBook is object type so you can not get property. try to convert it in to workbook.
hope this help.
Pascal BeerPosted May 21, 2012, 9:13 AM
Thanks for your reply. No also "WorkSheet" doesn't work.
If I set the Reference to the Microsofts Office Interop and then I cast the objBook and call:
objSheets = ((Workbook)objBook).Worksheets;
this works fine. But with Reflection not. Is this not a property? The strange thing is, that
objBooks.GetType().GetProperties()
returns an empty list!?
Regards
Pascal
Jignesh TrivediPosted May 21, 2012, 8:44 AM
I thing it is problem with name of the Property.
make sure name of the property is same as object.
I think Property is wrong in you case try with "WorkSheet"
hope this help.