正确释放WORD对象(COM组件) COMException: 被调用的对象已与其客户端断开连接
2014-03-13 15:42
330 查看
本来form method=post本页面
修改为其他页面 action=save.aspx后没问题
其他问题可参考以下:
引自:http://topic.csdn.net/u/20090108/17/f240cd4d-72cf-44bc-851e-cc587dd7e468.html
源问题:
详细内容:
System.Runtinm.InteropServices.COMException
被调用的对象已与其客户端断开连接。 (异常来自 HRESULT:0x80010108 (RPC_E_DISCONNECTED))
ErrorCode:-2147417848
Souce:Interop.Word
StackTrace:" 在 Word.ApplicationClass.Quit(Object& SaveChanges, Object& OriginalFormat, Object& RouteDocument)\r\n 在 WordOPTools.OpWord(Object& fileName,
Boolean issafe, String newpath)"
当处理的word内容比较大的时候报这个异常.
还请各位多多帮忙解决一下这个问题.
解决方法:
object nothing=System.Reflection.Missing.Value;
object optional=System.Reflection.Missing.Value;
object visible=true;
object saveChanges = true;
object NOTsaveChanges = false;
object docreadonly=true;
object originalFormat = System.Reflection.Missing.Value;
object routeDocument =System.Reflection.Missing.Value;
Word.ApplicationClass app=new Word.ApplicationClass();
object Fi=page.Server.MapPath(strC+"Template_temp/"+FileName);
Word.Document Doc=app.Documents.Open(ref Fi,ref optional,ref docreadonly,ref optional,ref optional,ref optional,ref optional,ref optional,ref optional,ref
optional,ref optional, ref visible);
Doc.SaveAs(ref strFileName,ref optional, ref optional, ref optional,ref optional, ref optional, ref optional,ref optional, ref optional, ref optional, ref
optional);
Doc.Close(ref NOTsaveChanges, ref originalFormat, ref routeDocument);
app.Quit(ref NOTsaveChanges, ref originalFormat, ref routeDocument);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
System.Runtime.InteropServices.Marshal.ReleaseComObject(Doc);
app=null;
Doc=null;
GC.Collect();
GC.Collect();
--------------------------------------------------------------
补充
------------摘自:http://blogs.geekdojo.net/richardhsu/archive/2003/11/14/281.aspx
Working with COM Exe in C#
Credit goes to Peter A. Bromberg for this one. In his article, he talks about creating dynamic excel workbooks in C# within an ASP.NET page.
What I learnt from the article was how we can properly release a COM object. There are two methods that we are gets the deallocation done. They are :-
1) GC.Collect(); // this one forces garbage collection
2) System.Runtime.InteropServices.Marshal.ReleaseComObject (object); // this one releases the passed in COM object wrapper instance
The pattern the author (Peter A. Bromberg) uses to create and release COM Wrapped Excel objects is :-
1) call GC.Collect() to force collection of existing COM Objects waiting to be released.
2) instantiate the COM Wrapped Excel objects (Workbook, Worksheet etc.)
3) do the thing...
4) call the Close() or Quit() methods on the objects when done.
5) call System.Runtime.InteropServices.Marshal.ReleaseComObject(object) once for each COM object created.
6) set each object variable to null.
7) call GC.Collect() again
8) be relieved and reminisce the cool VB6 way of doing the above (Set obj = Nothing)
Here is a slightly altered & annotated code fragment (in C#) that shows how it is done :-
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
// Step 1
GC.Collect();// clean up any other excel guys hangin' around...
// Step 2
oXL = new Excel.Application();
oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
// Step 3
// this part will actually be filling in the values into the sheet
fillValues(oSheet);
....
// Step 4
// Need all following code to clean up and extingush all references!!!
oWB.Close(null,null,null);
oXL.Workbooks.Close();
oXL.Quit();
// Step 5
System.Runtime.InteropServices.Marshal.ReleaseComObject (oXL);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oWB);
// Step 6
oSheet=null;
oWB=null;
oXL = null;
// Step 7
GC.Collect(); // force final cleanup!
Although I am yet to fully understand what goes on behind the scenes, I have used the above mention pattern, and it works. Excel exposes its functionality through a COM Exe. Maybe, we don't need to do all this for a COM Dll, but that is for later.
Published Friday, November 14, 2003 5:08 PM by richardhsu
Filed Under: Office
- VBA, C#
修改为其他页面 action=save.aspx后没问题
其他问题可参考以下:
引自:http://topic.csdn.net/u/20090108/17/f240cd4d-72cf-44bc-851e-cc587dd7e468.html
源问题:
详细内容:
System.Runtinm.InteropServices.COMException
被调用的对象已与其客户端断开连接。 (异常来自 HRESULT:0x80010108 (RPC_E_DISCONNECTED))
ErrorCode:-2147417848
Souce:Interop.Word
StackTrace:" 在 Word.ApplicationClass.Quit(Object& SaveChanges, Object& OriginalFormat, Object& RouteDocument)\r\n 在 WordOPTools.OpWord(Object& fileName,
Boolean issafe, String newpath)"
当处理的word内容比较大的时候报这个异常.
还请各位多多帮忙解决一下这个问题.
解决方法:
object nothing=System.Reflection.Missing.Value;
object optional=System.Reflection.Missing.Value;
object visible=true;
object saveChanges = true;
object NOTsaveChanges = false;
object docreadonly=true;
object originalFormat = System.Reflection.Missing.Value;
object routeDocument =System.Reflection.Missing.Value;
Word.ApplicationClass app=new Word.ApplicationClass();
object Fi=page.Server.MapPath(strC+"Template_temp/"+FileName);
Word.Document Doc=app.Documents.Open(ref Fi,ref optional,ref docreadonly,ref optional,ref optional,ref optional,ref optional,ref optional,ref optional,ref
optional,ref optional, ref visible);
Doc.SaveAs(ref strFileName,ref optional, ref optional, ref optional,ref optional, ref optional, ref optional,ref optional, ref optional, ref optional, ref
optional);
Doc.Close(ref NOTsaveChanges, ref originalFormat, ref routeDocument);
app.Quit(ref NOTsaveChanges, ref originalFormat, ref routeDocument);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
System.Runtime.InteropServices.Marshal.ReleaseComObject(Doc);
app=null;
Doc=null;
GC.Collect();
GC.Collect();
--------------------------------------------------------------
补充
------------摘自:http://blogs.geekdojo.net/richardhsu/archive/2003/11/14/281.aspx
Working with COM Exe in C#
Credit goes to Peter A. Bromberg for this one. In his article, he talks about creating dynamic excel workbooks in C# within an ASP.NET page.
What I learnt from the article was how we can properly release a COM object. There are two methods that we are gets the deallocation done. They are :-
1) GC.Collect(); // this one forces garbage collection
2) System.Runtime.InteropServices.Marshal.ReleaseComObject (object); // this one releases the passed in COM object wrapper instance
The pattern the author (Peter A. Bromberg) uses to create and release COM Wrapped Excel objects is :-
1) call GC.Collect() to force collection of existing COM Objects waiting to be released.
2) instantiate the COM Wrapped Excel objects (Workbook, Worksheet etc.)
3) do the thing...
4) call the Close() or Quit() methods on the objects when done.
5) call System.Runtime.InteropServices.Marshal.ReleaseComObject(object) once for each COM object created.
6) set each object variable to null.
7) call GC.Collect() again
8) be relieved and reminisce the cool VB6 way of doing the above (Set obj = Nothing)
Here is a slightly altered & annotated code fragment (in C#) that shows how it is done :-
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
// Step 1
GC.Collect();// clean up any other excel guys hangin' around...
// Step 2
oXL = new Excel.Application();
oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
// Step 3
// this part will actually be filling in the values into the sheet
fillValues(oSheet);
....
// Step 4
// Need all following code to clean up and extingush all references!!!
oWB.Close(null,null,null);
oXL.Workbooks.Close();
oXL.Quit();
// Step 5
System.Runtime.InteropServices.Marshal.ReleaseComObject (oXL);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oWB);
// Step 6
oSheet=null;
oWB=null;
oXL = null;
// Step 7
GC.Collect(); // force final cleanup!
Although I am yet to fully understand what goes on behind the scenes, I have used the above mention pattern, and it works. Excel exposes its functionality through a COM Exe. Maybe, we don't need to do all this for a COM Dll, but that is for later.
Published Friday, November 14, 2003 5:08 PM by richardhsu
Filed Under: Office
- VBA, C#
相关文章推荐
- 正确释放WORD对象(COM组件) COMException: 被调用的对象已与其客户端断开连接
- wp7模拟器开启不了,显示“被调用的对象已与其客户端断开连接”或者“接口”。并且提示有文件损坏的处理方法
- HTTP协议客户端和服务器交互的三次握手、持续连接、断开连接过程
- 什么情况下用“+”运算符进行字符串连接比调用StringBuffer/StringBuilder 对象的append方法连接字符串性能更好?
- C# Socket连接 无法访问已释放的对象
- 什么情况下用+运算符进行字符串连接比调用StringBuffer/StringBuilder对象的append方法连接字符串性能更好?
- VMware ESXi客户端连接控制台时提示"VMRC控制台连接已断开...正在尝试重新连接"的解决方法
- 一次http请求,谁会先断开TCP连接?什么情况下客户端先断,什么情况下服务端先断?
- JAVA NIO写服务端判断客户端断开连接的方法
- java调用com组件操作word使用总结(jacob)
- 客户端通过Ajax调用后台方法返回DataSet,Ilist,ilist,T,对象
- C# socket服务端判断 客户端已经断开连接的一个小办法
- Socket服务器 判断客户端连接断开
- Android 客户端调用基于XFire的webService方法传递自定义对象
- TCP客户端断开连接后,服务器连接处于CLOSE_WAIT状态之解决办法
- 如何处理socket连接后服务器端或客户端的断开
- C#编写socket客户端,服务器断开连接时客户端报异常
- 用FTP时务必注意,在下载文件的时候将对象的KeepAlive属性设置为false(即下载完成立即断开连接)
- C#在父窗口中调用子窗口的过程(无法访问已释放的对象)