C # force close the Excel process (when using Excel through COM)

Source: Internet
Author: User

In the previous project, the customer asked for an Excel report.

Because of the complexity of the report, we use the Excel template for creating the report first, and then write the data into the Excel template through COM (the disadvantage is that the deployed machine must have Excel ).

 

It was quite smooth at the beginning, and the report can be completed as required. However, it takes a long time for the server to slow down. After reading the Windows Process Manager, you can find that there are many excel.exe processes. The excel.exe process in the original cause is not properly shut down every time the compost‑called command is used.

 

The reason for this analysis is that the class in COM is not managed code after all, even if we release the new classes (such as Excel. Application, Excel. Workbooks, and so on) in the program ). However, GC of. net may not recycle these classes called through COM in time. As a result, many excel.exe processes reside in the operating system.

 

Since there are such problems, we need to forcibly recycle these processes to avoid the increasing number of processes on the server, and finally cause the system to crash.

This time, we will force the Kill process to release resources. If there is a better way, I hope you will not give me any advice!

 

This method encapsulates an ExcelInstances class to manually manage an Excel instance. The Code is as follows:

 

ExcelInstances
 Public class ExcelInstances
{
Private static Microsoft. Office. Interop. Excel. Application m_excelApp = null;
Private static Microsoft. Office. Interop. Excel. Workbooks m_excelWorkBooks = null;

[DllImport ("User32.dll")]
Public static extern int GetWindowThreadProcessId (IntPtr hWnd, out int Processid );

Private ExcelInstances ()
{
}



// After ExcelInstances is initialized, the corresponding COM instance is generated

Private static void Init ()
{
If (m_excelApp = null)
{
M_excelApp = new Microsoft. Office. Interop. Excel. Application ();
M_excelApp.DisplayAlerts = false;
M_excelApp.AlertBeforeOverwriting = false;
}

If (m_excelWorkBooks = null)
{
If (m_excelApp! = Null)
{
M_excelWorkBooks = m_excelApp.Workbooks;
}
}
}



// Auxiliary function-obtain the number of Excel processes in the current system

Public static int GetExcelProcessCount ()
{
Int iReturn = 0;
System. Diagnostics. Process [] pProcesses = null;

Try
{
PProcesses = System. Diagnostics. Process. GetProcesses ();

Foreach (System. Diagnostics. Process p in pProcesses)
{
If (string. Equals (p. ProcessName. ToString (), "EXCEL "))
{
IReturn ++;
}
}
}
Catch (Exception e)
{
Throw e;
}

Related Article

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.