简体   繁体   English

在C#应用程序中与Word的互操作非常慢

[英]Very slow interop with Word in c# application

I use VS2005 and I need to create many .doc files. 我使用VS2005,我需要创建许多.doc文件。 My computer(Intel c2d6600 with 2gb RAM) can convert with 10 files/minute it is very slow for me. 我的计算机(具有2GB RAM的英特尔c2d6600)每分钟可以转换10个文件,这对我来说非常慢。 What should I do to improve perfomance? 我应该怎么做才能提高性能?

My code: 我的代码:


oWord = new Word.Application();
oMissing = System.Reflection.Missing.Value;
oDoc = this._oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

...
//Do something
...

//save rtf
object fileName = this.FileRtf;
object fileFormat = Word.WdSaveFormat.wdFormatRTF;
object savechanges = false;
oDoc.SaveAs(ref fileName, ref fileFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
oWord.Quit(ref savechanges, ref oMissing, ref oMissing);

My psychic debugging powers tell me that you need to optimize the below code: 我灵敏的调试能力告诉我,您需要优化以下代码:

...
//Do something
...

Try to reduce number of calls to 尝试减少拨打电话

oWord = new Word.Application(); 
//and 
oWord.Quit(ref savechanges, ref oMissing, ref oMissing); 

In other words call them once and use one instance of Word for multiple files. 换句话说,一次调用它们,并将一个Word实例用于多个文件。

First, you should profile your application to find out, where the time is spent. 首先,您应该对应用程序进行概要分析,以找出花费的时间。

For is list of profilers, see this SO question . 有关分析器的列表,请参见此SO问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM