C# 应用程序中与 Word 的互操作非常慢
我使用VS2005,需要创建许多.doc 文件。 我的电脑(带有 2GB RAM 的 Intel c2d6600)可以每分钟转换 10 个文件,这对我来说非常慢。 我应该做什么来提高绩效?
我的代码:
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);
I use VS2005 and I need to create many .doc files.
My computer(Intel c2d6600 with 2gb RAM) can convert with 10 files/minute it is very slow for me.
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的心灵调试能力告诉我,你需要优化以下代码:
My psychic debugging powers tell me that you need to optimize the below code:
尝试减少调用次数。
换句话说,调用一次并使用一个 Word 实例处理多个文件。
Try to reduce number of calls to
In other words call them once and use one instance of Word for multiple files.
首先,您应该分析您的应用程序以了解时间花在哪里。
有关分析器的列表,请参阅此SO问题。
First, you should profile your application to find out, where the time is spent.
For is list of profilers, see this SO question.