C # Close Excel (close Excel process)

Source: Internet
Author: User

I have been troubled by how to close the Excel process when exporting an Excel file. I have used oexcelapp. quit (); also used GC collection, the results are not ideal, and later found that the kill process can be used, but the problem is that it is difficult to solve the problem of concurrent use of multiple people when the kill process, for example, if a person is exporting a table and then kill the Excel file, but another person is exporting the table at the same time, this will end the other Excel file. What we need to do now is how to kill the current process, let's take a look at it first.Code:

Oexcelapp. Quit ();

Publicmethod. Kill (oexcelapp); // call kill current Excel Process

Publicmethod. Kill () content is:

using system. runtime. interopservices;
public class publicmethod
{< br> Public publicmethod ()
{< br> //
// todo: add the constructor logic here
//
}< br> [dllimport ("user32.dll", charset = charset. auto)]
Public static extern int getwindowthreadprocessid (intptr hwnd, out int ID);
Public static void kill (Microsoft. office. interOP. excel. application Excel)
{< br> 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
}

}

 

In this way, we can close the current process Excel instead of killing all types of Excel processes,

 

Another new method:

If you find an Excel template and load it into an Excel application object at a time, if the Excel process exits, copy the template and clear the content, re-design the template content you want on this template, load the Excel application object of this template, and export the data. Then the Excel process will be very obedient to you,
I have tested that when many users in the system click to export an Excel report at the same time, there are only two or three processes. Normally, there is only one! The premise is that you need to find an obedient excel template.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.