Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Microsoft.Office.Interop.Excel将文件另存为PDF_C#_Com - Fatal编程技术网

C# 使用Microsoft.Office.Interop.Excel将文件另存为PDF

C# 使用Microsoft.Office.Interop.Excel将文件另存为PDF,c#,com,C#,Com,我正在使用Microsoft.Office.Interop.Excel.Workbooks.Open方法打开.XML文件,然后使用Microsoft.Office.Interop.Excel.Workbook.ExportAsFixedFormat方法将打开的.XML文件保存为.PDF文件进行发布 现在,在我的开发和预生产环境中,一切都运行良好,但在我们的生产服务器上,似乎在调用Microsoft.Office.Interop.Excel.Workbooks.Open时,方法执行会停止,甚至不执

我正在使用Microsoft.Office.Interop.Excel.Workbooks.Open方法打开.XML文件,然后使用Microsoft.Office.Interop.Excel.Workbook.ExportAsFixedFormat方法将打开的.XML文件保存为.PDF文件进行发布

现在,在我的开发和预生产环境中,一切都运行良好,但在我们的生产服务器上,似乎在调用Microsoft.Office.Interop.Excel.Workbooks.Open时,方法执行会停止,甚至不执行。。不存在我可以察觉到的任何异常或错误,即使在我的日志文件或事件查看器中也不存在。。显然,最终没有.PDF文件保存在任何地方

这是我的密码:

注意-我正在记录日志文件,以便能够查看部署后执行的代码行

我还通过将\用户和网络服务用户添加到访问权限默认值以及启动和激活权限,确保我拥有在组件服务管理下的COM安全选项卡中执行COM组件的权限。 如前所述


我想问的是,你们是否知道Open方法有什么问题。

我会考虑用它来读取excel文件

然后,这将允许您打印为PDF

它的开源和免费使用的商业应用程序


这不是一个完整的解决方案,但只是一个开始。然后,您就不能在服务器环境中使用Com了

。Microsoft目前不建议也不支持从任何无人参与、非交互式客户端应用程序或组件(包括ASP、ASP.NET、DCOM和NT服务)自动化Microsoft Office应用程序,因为在这种环境下运行Office时,Office可能会表现出不稳定的行为和/或死锁。你是对的,但由于我在我的预生产服务器上完美地使用了它,我非常有信心它会在我们的生产服务器上工作。。当时我知道微软的建议。。当我再次阅读所有内容时,我想我会尝试使用其中一种方法,看看它是否适合我。当时我正在尝试一种方法,但它不适合我,所以我使用了Office简介。。我知道有很多XML 2 PDF解决方案,我将在@seth flowers comment中尝试微软的方法后再尝试
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);
}