1、对excel操作做成一个函数,然后调用此函数。在函数中调用gc.collect();无用,因为gc不回收调用自己的那一段代码块!
2、在函数的下面调用gc.collect();语句。你会发现excel进程没有了!
例如:
private void import() {
     excel.application myexcel  = new excel.application();
     myexcel.workbooks.add(openfiledialog1.filename);
    //........
   //读取excel文件,导入到数据库.
   //清除excel垃圾进程
   myexcel.workbooks.close(); 
   myexcel.quit(); 
   system.runtime.interopservices.marshal.releasecomobject(myexcel);
   myexcel = null;
}
 private void excelimport() {
   import();
   gc.collect();
 }
//以下按button1按钮,使用多线程读取excel文件,导入到数据库.
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(); 
    }
  }
新闻热点
疑难解答
图片精选