Use C # To write winform, write the data in the Excel file to the database, and export the data in the DataGrid to the Excel format. The Excel Memory leakage is finally found. When the application does not exit, there is always an Excel process that cannot be cleared! Finding answers online is useless! No matter what the process of killing the Excel file is, it is not the most effective method! The most effective method is the following:
1. Create a function for Excel operations and call this function. Calling GC. Collect () in the function is useless because GC does not recycle the code block that calls itself!
2. Call the GC. Collect (); statement under the function. You will find that the Excel process is gone!
For example:
Private void import (){
Excel. Application myexcel = new excel. Application ();
Myexcel. workbooks. Add (openfiledialog1.filename );
//........
// Read the Excel file and import it to the database.
// Clear the Excel spam Process
Myexcel. workbooks. Close ();
Myexcel. Quit ();
System. runtime. interopservices. Marshal. releasecomobject (myexcel );
Myexcel = NULL;
}
Private void excelimport (){
Import ();
GC. Collect ();
}
// Press the button1 button below to read the Excel file using multiple threads and import it to the database.
Private void button#click (Object sender, system. eventargs e ){
If (openfiledialog1.showdialog () = dialogresult. OK ){
System. Threading. thread t = new system. Threading. Thread (new system. Threading. threadstart (excelimport ));
T. Start ();
}
}