1、對excel操作做成一個函數(shù),然后調(diào)用此函數(shù)。在函數(shù)中調(diào)用gc.collect();無用,因為gc不回收調(diào)用自己的那一段代碼塊!
2、在函數(shù)的下面調(diào)用gc.collect();語句。你會發(fā)現(xiàn)excel進(jìn)程沒有了!
例如:
private void import() {
excel.application myexcel = new excel.application();
myexcel.workbooks.add(openfiledialog1.filename);
//........
//讀取excel文件,導(dǎo)入到數(shù)據(jù)庫.
//清除excel垃圾進(jìn)程
myexcel.workbooks.close();
myexcel.quit();
system.runtime.interopservices.marshal.releasecomobject(myexcel);
myexcel = null;
}
private void excelimport() {
import();
gc.collect();
}
//以下按button1按鈕,使用多線程讀取excel文件,導(dǎo)入到數(shù)據(jù)庫.
private void button1_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();
}
}
新聞熱點
疑難解答
圖片精選