6
Answers

Excel with Reflection

Pascal Beer

Pascal Beer

12y
2.7k
1
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)