【发布时间】:2012-10-25 08:42:19
【问题描述】:
我正在使用 Microsoft.Office.Interop.Excel.Workbooks.Open() 方法打开一个 .XML 文件,然后使用 Microsoft.Office.Interop.Excel.Workbook.ExportAsFixedFormat() 将打开的 .XML 文件发布(保存)为 .PDF 文件的方法。
现在在我的开发和预生产环境中一切正常,但在我们的生产服务器上,似乎当调用 Microsoft.Office.Interop.Excel.Workbooks.Open() 时然后方法执行被停止甚至不执行..(没有任何我可以知道的异常或任何错误,即使不在我的日志文件或事件查看器中..)并且最终没有保存 .PDF 文件显然在任何地方。
这是我的代码:
using ExcelApp = Microsoft.Office.Interop.Excel;
public static void ToPdf()
{
// Declare the Excel Application object and set it in null state
ExcelApp.Application activeExcel = null;
// Declare the Workbook to be used within the Excel Application object
ExcelApp.Workbook openWorkBook = null;
// Used for passing parameter for the attribute of opening the .XML file
object tMiss = Type.Missing;
// .XML file location
string xmlPathName = "C:/Windows/Temp/XmlFile.xml";
try
{
// Instanitating the Excel.Application to a new valid Instance object
activeExcel = new ExcelApp.Application();
// Open Excel as Hiden
activeExcel.Visible = false;
// Open Excel w/o alerts
activeExcel.DisplayAlerts = false;
//
// Log this line
//
LogTxt.LogCreator("Set Excel GUI to be open Hiden with no Alerts");
// Open an Excel instance and passing the .XML file Path
openWorkBook = activeExcel.Workbooks.Open(xmlPathName, 0, false, tMiss, tMiss,
tMiss, true, tMiss, tMiss, tMiss,
true, tMiss, false, false);
//
// Log this line
//
LogTxt.LogCreator("Excel Application Opend");
// Publishing the opened workbook (.xml file) as .pdf file
openWorkBook.ExportAsFixedFormat(ExcelApp.XlFixedFormatType.xlTypePDF, pdfPathName);
//
// Log this line
//
LogTxt.LogCreator("Excel to .PDF published");
}
catch (Exception ee)
{
LogTxt.LogCreator(string.Format("Error is {0}", ee.InnerException.Message));
}
finally
{
// Flag for finding the Excel process or not
//bool foundExcel = false;
if (openWorkBook != null)
{
// Closing the workbook
openWorkBook.Close(false, tMiss, tMiss);
// here we say that this object is not going to be called anymore
Marshal.ReleaseComObject(openWorkBook);
}
if (activeExcel != null)
{
// Closing the Excel
activeExcel.Quit();
// here we say that this object is not going to be called anymore
Marshal.ReleaseComObject(activeExcel);
//
// Log this line
//
LogTxt.LogCreator("Excel Procces Closed");
}
GC.GetTotalMemory(false);
// Calling to GC go through all gen
GC.Collect();
// Stops the corrent thread untill the Finalizers empty the queue
GC.WaitForPendingFinalizers();
// Calling the GC to go through all gen
GC.Collect();
GC.GetTotalMemory(true);
}
注意 - 我正在登录到我的日志文件,以便能够查看部署后执行的代码行。
我还确保我有权在组件服务管理下的 COM 安全选项卡中执行 COM 组件,方法是将 \Users 和网络服务用户添加到访问权限默认值以及启动和激活权限。
正如here所解释的那样。
我要问的是,你们是否对理解 Open() 方法的问题有任何想法。
【问题讨论】:
-
Office Interop is not supported on a server environment。 Microsoft 目前不推荐也不支持任何无人值守、非交互式客户端应用程序或组件(包括 ASP、ASP.NET、DCOM 和 NT 服务)的 Microsoft Office 应用程序自动化,因为 Office 可能表现出不稳定的行为和/或在此环境中运行 Office 时出现死锁。
-
你是对的,但是当我在我的预生产服务器上完美使用它时,我非常有信心它可以在我们的生产服务器上工作。我当时知道微软的建议。因为我正在再次阅读所有内容,我想我会尝试使用其中一种方法,看看它是否适合我。