Pascal Beer

Pascal Beer

  • NA
  • 7
  • 3.9k

Excel with Reflection

May 21 2012 4:05 AM
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


Answers (6)