哪位兄弟帮我检查一下代码,kill掉程序执行完后的excel的进程,进程在任务管理器中可查到,谢谢了!!!!! excel.quit();??不过进程那边好像还是要等程序退出才会消失哦,不清楚,等待高手帮忙up 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 try{workbook.SaveAs(this.strExcelFilePath,Excel.XlFileFormat.xlWorkbookNormal,oMissing,oMissing,oMissing,oMissing, Excel.XlSaveAsAccessMode.xlShared,oMissing,oMissing,oMissing,oMissing,oMissing); } catch( Exception e ) { } finally { if(range != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(range); } if(worksheet != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet); } if(workbook != null) { workbook.Close(isSave,oMissing,oMissing); System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); } if(excel != null) { excel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(excel); } workbook = null; worksheet = null; range = null; excel = null; GC.Collect(); } 还有需要在DCOM中配置一下Excel的访问权限 将对excel的操作写到一个函数里,在调用函数后再执行System.GC.Collect();这样应该就可以了。如:private void Button1_Click(object sender, System.EventArgs e){ string strFileName = ExpExcel(); System.GC.Collect(); Response.Redirect(strFileName);} private string ExpExcel(){string filename=Server.MapPath(".")+"\\bin\\af.xls";FileInfo sTemplate=new FileInfo(Server.MapPath(".")+"\\bin\\normal.xls");sTemplate.CopyTo(filename,true);object missing=Missing.Value;Excel.Application excel = null;Excel.Workbooks workbooks = null;Excel.Workbook workbook = null;Excel.Sheets sheets = null;Excel.Worksheet worksheet = null;Excel.Range oCells=null; excel = new Excel.ApplicationClass();excel.Visible = false;excel.DisplayAlerts = false;workbooks = excel.Workbooks;workbook = workbooks.Open(filename,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing, missing,missing);sheets = workbook.Worksheets;worksheet = (Excel.Worksheet)sheets[1];worksheet.Name = "First Sheet";oCells = worksheet.Cells; worksheet.Cells[5,5]="'"+"99999999";workbook.Save (); //worksheet.SaveAs(filename); // workbook.Close();//Quit Excel and thoroughly deallocate everythingexcel.Quit();System.Runtime.InteropServices.Marshal .ReleaseComObject(oCells) ;System.Runtime.InteropServices.Marshal .ReleaseComObject(worksheet);System.Runtime.InteropServices.Marshal .ReleaseComObject(sheets) ;System.Runtime.InteropServices.Marshal .ReleaseComObject(workbook);System.Runtime.InteropServices.Marshal .ReleaseComObject(workbooks) ;System.Runtime.InteropServices.Marshal .ReleaseComObject(excel);excel = null ; workbooks = null ; workbook = null;sheets = null ; worksheet = null ; oCells = null;return filename;} 得用垃圾回收机制回收,我用winForm时,也出现这个问题后来在csdn上找了一贴,就解决了,你搜搜 gesnpt(gesnpt) 的可以,用两个函数 窗体间的数据通信问题 在java或c#中遇到循环的题目的分析? 数据类型 Money 是哪个命名空间的? 求解决方案:Web收件箱(ASP.net + POP3收信) [求助]DataGridView里怎样获得选定单元格中的值,谢谢! C# 实时更新 热键不响应问题!!! 请问在C#中如何调用.exe文件?十分感谢! AhBian(阿扁)看过来 两个窗体传递参数 在C#里用到.JS文件的问题 Calendar 点击已经选择的值
workbook.SaveAs(this.strExcelFilePath,Excel.XlFileFormat.xlWorkbookNormal,oMissing,oMissing,oMissing,oMissing,
Excel.XlSaveAsAccessMode.xlShared,oMissing,oMissing,oMissing,oMissing,oMissing);
}
catch( Exception e )
{
}
finally
{
if(range != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
}
if(worksheet != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
}
if(workbook != null)
{
workbook.Close(isSave,oMissing,oMissing);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
}
if(excel != null)
{
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
}
workbook = null;
worksheet = null;
range = null;
excel = null;
GC.Collect();
}
这样应该就可以了。
如:
private void Button1_Click(object sender, System.EventArgs e)
{
string strFileName = ExpExcel();
System.GC.Collect();
Response.Redirect(strFileName);
} private string ExpExcel()
{
string filename=Server.MapPath(".")+"\\bin\\af.xls";
FileInfo sTemplate=new FileInfo(Server.MapPath(".")+"\\bin\\normal.xls");
sTemplate.CopyTo(filename,true);
object missing=Missing.Value;
Excel.Application excel = null;
Excel.Workbooks workbooks = null;
Excel.Workbook workbook = null;
Excel.Sheets sheets = null;
Excel.Worksheet worksheet = null;
Excel.Range oCells=null;
excel = new Excel.ApplicationClass();
excel.Visible = false;
excel.DisplayAlerts = false;workbooks = excel.Workbooks;
workbook = workbooks.Open(filename,missing,missing,missing,missing,
missing,missing,missing,missing,missing,missing, missing,missing);
sheets = workbook.Worksheets;
worksheet = (Excel.Worksheet)sheets[1];
worksheet.Name = "First Sheet";
oCells = worksheet.Cells; worksheet.Cells[5,5]="'"+"99999999";
workbook.Save ();
//worksheet.SaveAs(filename);
// workbook.Close();//Quit Excel and thoroughly deallocate everything
excel.Quit();
System.Runtime.InteropServices.Marshal .ReleaseComObject(oCells) ;
System.Runtime.InteropServices.Marshal .ReleaseComObject(worksheet);
System.Runtime.InteropServices.Marshal .ReleaseComObject(sheets) ;
System.Runtime.InteropServices.Marshal .ReleaseComObject(workbook);
System.Runtime.InteropServices.Marshal .ReleaseComObject(workbooks) ;
System.Runtime.InteropServices.Marshal .ReleaseComObject(excel);
excel = null ; workbooks = null ; workbook = null;
sheets = null ; worksheet = null ; oCells = null;return filename;
}