I saw the farmer's uncle. C # operation Excel Heterodoxy [C # | Excel] "After, accidentally think of 2 years ago to do a small project, I also encountered the process of Excel can not end this strange problem, eventually resolved, in fact, the principle of the solution is very simple, Excel is a very special thing, all of its operations are exclusive, Therefore, it is necessary to strictly carry out the release of resources. In order to better communicate with you, but also to help those who are confused programmers friends, the following is attached to my previous small code, in order to be able to quickly and easily explain the problem, the code has been deleted, only to preserve the integrity of the structure, but do not guarantee that the code can be successfully compiled, the
Namespaces that need to be referenced
Using EXECL = Microsoft.Office.Interop.Excel;
Try
{
Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application ();
Microsoft.Office.Interop.Excel.Workbook workbook = Excel. Workbooks.Open (Lujing2, System.Type.Missing, False, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
Excel. Visible = true;
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet) workbook. Worksheets.get_item (1);
Start Excel operation
if (Excel. Activeworkbook.saved = = False)
{
Excel. Activeworkbook.save ();
}
Excel. Quit ();
Excel = null;
Application.exit ();
Gc. Collect (System.GC.GetGeneration (worksheet));
Gc. Collect (System.GC.GetGeneration (workbook));
Gc. Collect (System.GC.GetGeneration (Excel));
}
Catch
{
}
Finally
{
Gc. Collect ();
}
At the same time, there is a more contentious issue here, I hereby declare: Microsoft strongly recommends against the Gc.collect method to enforce the spam phone, because it will hinder the way the GC itself works. Consider using the Gc.collect method to invoke the collector only when you are explicitly aware of a large number of objects stopping references.
Microsoft's COM for Excel is still very good, and ask friends not to blame it.
On the reply of the Netizen's supplement:
Whether or not Excel is a managed resource, I think this is a different level of things, the key is that our program is to invoke COM to operate on Excel, our COM should not be unmanaged resources bar.
Another example: the Web in the common file upload and file packaging downloads, these files are managed or unmanaged resources, in fact, nothing, more talk about what to release and not release the problem.
Our program calls com,com to perform operations with Excel, which can be released as normal code specifications.
Use kill programs, just like Excel is suddenly ended, may produce fragments, and so on, serious Excel will also be damaged, and even more, Excel's process can not kill, but it will cause your program abnormal, resulting in unpredictable loss.
For example, what does COM do, just like a normal user opens an Excel, performs some operations, and then closes Excel, we do not need to let third party software to perform what memory release work.
Different levels of things, do not put a piece, otherwise you will make a mess.
I do not know whether I speak clearly enough, or I understand the problem, welcome to point out that we explore together.