From: http://www.cnblogs.com/xuanhun/archive/2010/04/29/1724500.html
...
4. "object reference not set to object instance" and server deployment
Everything on the client runs normally. Next, deploy the DLL on the server. However, the server does not have to install the office at all. I have been searching for a long time on the website and have not found a solution for this method. I had to install the office. I thought everything was fine, but every time I was prompted "I didn't set the object reference to the object instance ". The example is known only when an exception is thrown.ProgramThe main fields in are empty, that is, the local com is not successfully called. FindHttp://www.cnblogs.com/Mainz/archive/2009/11/11/microsoft_office_interop_excel.htmlArticleAs a result, the following operations are performed.
put excel on the server. copy the EXE file to the local machine and start tlbimp , run the following command: tlbimp/out: InterOP. excel. DLL excel.exe . Generate InterOP. excel. DLL .
RemoveMicrosoft. Office. InterOP. Excel. dll reference, addInterOP. Excel. dll, SetUsing Excel = Microsoft. Office. InterOP. Excel, Which isUsing Excel = InterOP. Excel, There is no problem with running the program locally. When the server program is updated again, a new exception occurs:Failed to retrieve components whose CLSID is {00024500-0000-0000-c000-000000000046} in the com class factory because of the following error:80080005.
5. "80080005 exceptions" and permission Configuration
"80080005 exception" is caused by insufficient permissions of the application to operate the COM component. The following operations are available:
1) control panel-> Administrative Tools-> component services-> Computer-> my computer-> DCOM-> Microsoft Excel applications
2) Click properties to open the Properties dialog box for this application.
3) Click the ID tab and select an interactive user.
4) Click the Default Security tab. Set the access permissions of ASP. NET users on the current server.
5) Click Edit default value for the startup permission. Set the access permissions of ASP. NET users.
At this point, the program runs successfully.
6. Close the Excel Process
If you export an Excel file in this way, the server starts an Excel process. This may cause two problems. One is server resource consumption, and the other is that when the number of processes reaches the upper limit, an exception is thrown and the com call fails. At this time, we need to try to end the Excel process. I used the process kill method, similar to the following practice:
Private void doexcel ()
{
Microsoft. Office. InterOP. Excel. Application application = new Microsoft. Office. InterOP. Excel. Application ();
// Release all referenced resources here
Application. Quit ();
Killexcel (application );
}
[Dllimport ("user32.dll", charset = charset. Auto)]
Public static extern int getwindowthreadprocessid (intptr hwnd, out int ID );
Public static void killexcel (Microsoft. Office. InterOP. Excel. Application Excel)
{
Intptr T = new intptr (Excel. hwnd); // obtain the handle. The specific function is to obtain the memory entry.
Int K = 0;
Getwindowthreadprocessid (T, out k); // obtain the unique identifier K of the process.
System. Diagnostics. PROCESS p = system. Diagnostics. process. getprocpolicyid (k); // get a reference to process K
P. Kill (); // closes process K
}
7. Other options
There are more than one way to generate an Excel file, such as using openxml. Let's give you an introduction. I will learn from you.