国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

.net中清除EXCEL進(jìn)程最有效的方法

2024-07-10 12:58:43
字體:
供稿:網(wǎng)友
,歡迎訪問網(wǎng)頁設(shè)計愛好者web開發(fā)。 最近用c#寫winform,將excel文件中的數(shù)據(jù)寫入數(shù)據(jù)庫中,將datagrid中的數(shù)據(jù)導(dǎo)出為excel格式。最后發(fā)現(xiàn)excel內(nèi)存泄漏,在應(yīng)用程序不退出的情況下,總是有一個excel進(jìn)程不能清除!在網(wǎng)上找了許多答案,都是無用的答案!什么不管三七二十一殺excel進(jìn)程啦,不是最有效的方法!其實最有效的方法就是下面這個方法:

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();
    }
  }


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 浪卡子县| 马公市| 岑溪市| 仙居县| 鄂伦春自治旗| 福州市| 本溪市| 仪陇县| 仙游县| 洛隆县| 顺昌县| 通州区| 三明市| 萍乡市| 梅河口市| 巩义市| 沂水县| 高安市| 邯郸县| 阿合奇县| 若羌县| 茂名市| 绥江县| 金门县| 奉节县| 鲜城| 营山县| 尖扎县| 巴林左旗| 永福县| 横峰县| 庆安县| 明溪县| 高安市| 西丰县| 米易县| 剑河县| 辽阳县| 双鸭山市| 桐城市| 高陵县|