0

I found this code on Internet to create a Excel file using Interop library:

object misValue = System.Reflection.Missing.Value;
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com";
xlWorkBook.SaveAs("csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
MessageBox.Show("Excel file created , you can find the file c:\\csharp-Excel.xls");

But I'm getting this error when I execute the code:

Object reference not set to an instance of an object

on this line:

xlWorkBook = xlApp.Workbooks.Add(misValue);

EDIT this is the stack trace:

System.NullReferenceException was unhandled
 HResult=-2147467261
 Message=Object reference not set to an instance of an object.
 Source=CMApp
 StackTrace:
 at WindowsFormsApplication1.ExcelDBUserControl.button3_Click(Object sender, EventArgs e) in d:\Private\Dropbox\Work\ClanMovil21Comunicaciones\Apps\WindowsFormsApplication1\WindowsFormsApplication1\ExcelDBUserControl.cs:line 332
 at System.Windows.Forms.Control.OnClick(EventArgs e)
 at System.Windows.Forms.Button.OnClick(EventArgs e)
 at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
 at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
 at System.Windows.Forms.Control.WndProc(Message& m)
 at System.Windows.Forms.ButtonBase.WndProc(Message& m)
 at System.Windows.Forms.Button.WndProc(Message& m)
 at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
 at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
 at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
 at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
 at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
 at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
 at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
 at System.Windows.Forms.Application.Run(Form mainForm)
 at WindowsFormsApplication1.Program.Main() in d:\Private\Dropbox\Work\ClanMovil21Comunicaciones\Apps\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 20
 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
 at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
 at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
 at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
 at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
 at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
 at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
 at System.Threading.ThreadHelper.ThreadStart()
 InnerException: 

What's wrong?

asked Apr 22, 2013 at 19:32
5
  • Where do you get the error? What's the stack trace? Commented Apr 22, 2013 at 19:32
  • @SLaks sorry I edited the code and add the line where code break Commented Apr 22, 2013 at 19:33
  • @SLaks Excel.Application xlApp; I defined xlApp var already Commented Apr 22, 2013 at 19:36
  • I would like to discourage you from creating Excel files using Interop -- it is almost always a bad idea. How about creating an excel file using one of the Excel XML formats? e.g. en.wikipedia.org/wiki/… Commented Apr 22, 2013 at 19:37
  • 1
    @Reynier if you just defined the xlApp like you say you do then it obviously will be null and cause the exception. You need to put something in that variable, such as Steve's answer below Commented Apr 22, 2013 at 19:39

1 Answer 1

1

Probably missing in copy/paste?

Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
answered Apr 22, 2013 at 19:37

2 Comments

you're right that was the problem also I'm newbie with C# but I'll take a look to mikey suggestion, I notice that Interop is to slow and I'll create a huge Excel files
It is not only slow, but has a serious drawback. It requires Office installed on the customer PC and sometimes this is not available.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.