【发布时间】:2011-07-20 21:51:43
【问题描述】:
我正在编写要在服务器上运行的 C# Window 服务(与 OFFICE 一起安装) 我需要将 MS Word DOC 转换为 RTF 文件并将其加载到 RICHTEXTBOX 中,然后将 RTF 字符串和明文字符串获取到 DB(获取明文字符串以进行全文索引以允许用户搜索)
我使用以下代码在服务中执行转换, 但是,它在线上发生了错误 newApp.Documents.Open "内存不足,请立即保存文档"
我检查了服务器任务管理器,我发现 Winword.exe 正在加载大量内存(例如 60~70 Mb)并且它没有退出(嗯,它得到了异常.....>_
我尝试在同一台机器上使用 Windows Form 运行相同的代码,但没有出现错误。 并且该服务已设置为以管理员身份运行。
private void doc2rtf(object Source, object Target)
{
//Creating the instance of Word Application
Word.Application newApp = new Word.Application();
newApp.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable;
newApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
// specifying the Source & Target file names
// Use for the parameter whose type are not known or
// say Missing
object Unknown = Type.Missing;
object objReadOnly = true;
object objFalse = false;
try
{
// Source document open here
// Additional Parameters are not known so that are
// set as a missing type
lw.writeLog(LogWriter.logType.DEBUG, "before newApp.Documents.Open", Source.ToString());
newApp.Documents.Open(ref Source, ref Unknown,
ref objReadOnly, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown, ref Unknown);
lw.writeLog(LogWriter.logType.DEBUG, "after newApp.Documents.Open", Source.ToString());
// Specifying the format in which you want the output file
object format = Word.WdSaveFormat.wdFormatRTF;
//check header footer exists.
lw.writeLog(LogWriter.logType.DEBUG, "before newApp.ActiveDocument.SaveAs", Target.ToString());
//Changing the format of the document
newApp.ActiveDocument.SaveAs(ref Target, ref format,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown);
lw.writeLog(LogWriter.logType.DEBUG, "after newApp.ActiveDocument.SaveAs", Target.ToString());
}
catch (Exception e)
{
lw.writeLog(LogWriter.logType.ERROR, e.Message, "doc2rtf");
}
finally
{
lw.writeLog(LogWriter.logType.DEBUG, "before newApp.ActiveDocument.Close(", "");
newApp.ActiveDocument.Close(ref objFalse, ref Unknown, ref Unknown);
// for closing the application
lw.writeLog(LogWriter.logType.DEBUG, "after newApp.ActiveDocument.Close(", "");
lw.writeLog(LogWriter.logType.DEBUG, "before newApp.ActiveDocument.Quit(", "");
newApp.Quit(ref objFalse, ref Unknown, ref Unknown);
lw.writeLog(LogWriter.logType.DEBUG, "after newApp.ActiveDocument.Quit(", "");
newApp = null;
GC.Collect();
}
}
【问题讨论】:
-
Word 并不打算在服务器上下文中运行(上次我检查时,也没有获得该用途的许可)。有第三方应用可以处理word文档。
-
有点兴奋,不是吗?我们无法知道该服务是否被多个用户同时使用,这似乎是微软希望您使用他们的办公网络服务而不是他们的客户端产品的时候。但是,发现问题非常好。 +1