解决C#使用Microsoft.Office.Interop.Excel操作Excel后进程一直存在的问题
2015-04-23 14:33
816 查看
This resolved the issue for me. Your code becomes:
And then release all those objects, (Note: All the com object should be release, othewise the excel process won't be closed.)
I wrap this in a
public Excel.Application excelApp = new Excel.Application(); public Excel.Workbooks workbooks; public Excel.Workbook excelBook; workbooks = excelApp.Workbooks; excelBook = workbooks.Add(@"C:/pape.xltx");
... Excel.Sheets sheets = excelBook.Worksheets; Excel.Worksheet excelSheet = (Worksheet)(sheets[1]); excelSheet.DisplayRightToLeft = true; Range rng; rng = excelSheet.get_Range("C2"); rng.Value2 = txtName.Text;
And then release all those objects, (Note: All the com object should be release, othewise the excel process won't be closed.)
System.Runtime.InteropServices.Marshal.ReleaseComObject(rng); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelSheet); System.Runtime.InteropServices.Marshal.ReleaseComObject(sheets); excelBook .Save(); excelBook .Close(true); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook); System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks); excelApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
I wrap this in a
try {} finally {}to ensure everything gets released even if something goes wrong (what could possibly go wrong?) e.g.
public Excel.Application excelApp = null; public Excel.Workbooks workbooks = null; ... try { excelApp = new Excel.Application(); workbooks = excelApp.Workbooks; ... } finally { ... if (workbooks != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks); excelApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp); }
相关文章推荐
- C#操作EXCEL--使用Microsoft.Office.Interop.Excel,来自.netV1.2
- c#操作excel方式三:使用Microsoft.Office.Interop.Excel.dll读取Excel文件
- c#操作excel方式三:使用Microsoft.Office.Interop.Excel.dll读取Excel文件
- c#操作excel方式三:使用Microsoft.Office.Interop.Excel.dll读取Excel文件
- c#操作excel方式三:使用Microsoft.Office.Interop.Excel.dll读取Excel文件
- c#操作excel方式三:使用Microsoft.Office.Interop.Excel.dll读取Excel文件
- c#操作excel方式三:使用Microsoft.Office.Interop.Excel.dll读取Excel文件
- C#与excel互操作 C#与excel互操作的错误无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的 COM 对象强制 如果您使用的电脑
- C#使用Microsoft.Office.Interpo操作EXCEL遇到的问题总结
- C#操作Word导致 “Microsoft.Office.Interop.Word.ApplicationClass”的 COM 组件错误的解决方法
- C#引用Office.word出错的解决办法-无法嵌入互操作类型“Microsoft.Office.Interop.Word.ApplicationClass” 【转】
- C#导出到Excel——无法嵌入互操作类型“Microsoft.Office.Interop.Excel.ApplicationClass”。请改用适用的接口。
- IIS中使用Microsoft.Office.Interop.Excel 常见问题:RPC 服务器不可用。 (异常来自 HRESULT:0x800706BA) 的异常。等
- 数据转换excel操作 Microsoft.Office.Interop.Excel.dll的使用
- 解决在IIS中调用Microsoft Office Excel组件后进程无法正常退出的问题
- 【转载】Excel操作 Microsoft.Office.Interop.Excel.dll的使用
- 使用Microsoft.Office.Interop.Excel时,64位问题
- 转:C#编程读取Excel文件,引用Microsoft.Office.Interop.Excel出现的问题
- C#操作Excel完成后进程上还存在Excel进程的问题
- C#操作Office文件,成功释放,但读取文件时提示"文件***正由另一进程使用,因此该进程无法访问此文件"的解决办法