C# 用于网站(文件转换)的Microsoft.Office.Interop安全吗?
我正在编写一个网站,用户必须添加报告(Word文档),为了能够查看它们,我将*.doc转换为*.pdf,然后通过pdf.js显示它们。对于转换,我使用Microsoft.Office.Interop.Word。代码看起来像C# 用于网站(文件转换)的Microsoft.Office.Interop安全吗?,c#,asp.net,security,ms-word,ms-office,C#,Asp.net,Security,Ms Word,Ms Office,我正在编写一个网站,用户必须添加报告(Word文档),为了能够查看它们,我将*.doc转换为*.pdf,然后通过pdf.js显示它们。对于转换,我使用Microsoft.Office.Interop.Word。代码看起来像 public void ConvertDocument(string PATH) { FileInfo FILE = new FileInfo(PATH); if (FILE.Extension.ToLower() == ".doc"
public void ConvertDocument(string PATH)
{
FileInfo FILE = new FileInfo(PATH);
if (FILE.Extension.ToLower() == ".doc" || FILE.Extension.ToLower() == ".docx" || FILE.Extension.ToLower() == ".docm" || FILE.Extension.ToLower() == ".dotx" || FILE.Extension.ToLower() == ".dotm")
{
if (FILE.Length == 0)
{
return;
}
object oMissing = System.Reflection.Missing.Value;
Word.Application word = new Word.Application();
try
{
word.Visible = false;
word.ScreenUpdating = false;
Object filename = (Object)FILE.FullName;
Word.Document doc = word.Documents.Open(ref filename, 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, ref oMissing);
try
{
doc.Activate();
object outputFileName = FILE.FullName.Replace(FILE.Extension, ".PDF");
doc.SaveAs(ref outputFileName, Word.WdSaveFormat.wdFormatPDF, 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);
}
finally
{
object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
((Word._Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
doc = null;
}
}
finally
{
((Word._Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
word = null;
}
File.Delete(PATH);
}
}
微软的答案是否定的: Microsoft目前不推荐,也不支持, 从无人值守的环境中实现Microsoft Office应用程序的自动化, 非交互式客户端应用程序或组件(包括ASP, NET、DCOM和NT服务),因为Office可能表现出不稳定 在此环境中运行Office时的行为和/或死锁 从 根据经验,以下是我们遇到的问题:
- 当进程意外存在时,会留下大量Word实例,并且永远不会清理
- 在处理会话中途关机后,Word installation已损坏
- 它的可扩展性不太好——Word是一个大型桌面应用程序,它的功能非常出色;但是,它并不真正适用于您使用它的流程,因此,打开大量实例将消耗应用程序可以使用的资源