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

VulpesRank #55 · 29.2K reputationPosted May 21, 2012, 6:56 AM EDT
To obtain the first WorkSheet, try it like this:
Jignesh TrivediRank #16 · 50.3K reputationPosted May 21, 2012, 4:44 AM EDT
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.
Pascal Beer7 reputationPosted May 21, 2012, 5:13 AM EDT
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 TrivediRank #16 · 50.3K reputationPosted May 21, 2012, 5:17 AM EDT
I thing objBook is object type so you can not get property. try to convert it in to workbook.
hope this help.
Pascal Beer7 reputationPosted May 21, 2012, 6:05 AM EDT
Pascal Beer7 reputationPosted May 21, 2012, 8:43 AM EDT
Have a nice day
Pascal