简体   繁体   English

通过使用Microsoft.Office.Interop.Excel将文件另存为PDF

[英]Saving file as PDF by using Microsoft.Office.Interop.Excel

I'm using the Microsoft.Office.Interop.Excel.Workbooks.Open() method for opening an .XML file and then using the Microsoft.Office.Interop.Excel.Workbook.ExportAsFixedFormat() method for publishing (saving) the opened .XML file as a .PDF file. 我正在使用Microsoft.Office.Interop.Excel.Workbooks.Open()方法打开.XML文件,然后使用Microsoft.Office.Interop.Excel.Workbook.ExportAsFixedFormat()方法发布(保存)打开的.XML文件作为.PDF文件。

Now on my Development and Preproduction environments everything is working well, but on our Production server it's seems that when the call for Microsoft.Office.Interop.Excel.Workbooks.Open() is made the method execution is then sopped or even not executed.. (W/O any exception or any error that I can be aware of, even not in my log file or in the event viewer..) and eventually there is no .PDF file saved anywhere obviously. 现在,在我的开发和预生产环境中,一切都运行良好,但是在生产服务器上,似乎是在调用Microsoft.Office.Interop.Excel.Workbooks.Open()时 ,该方法的执行随后被暂停甚至不执行。 。(不会发现任何异常或任何我可以知道的错误,即使不在我的日志文件或事件查看器中也没有。),最终,显然没有任何地方保存.PDF文件。

Here is my code: 这是我的代码:

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);
}

Note - I'm logging to my log file so I'll be able to see what line of code is executed after deploy. 注意-我正在登录我的日志文件,因此可以查看部署后执行的代码行。

I've also ensured that I've the rights for executing COM components in the COM Security tab under Component Services management by adding the \\Users and the Network Service users to the Access Permissions Defaults and to Launch and Activation Permissions. 我还通过将\\ Users和Network Service用户添加到Access Permissions Defaults和Launch and Activation Permissions中,将\\ Users和Network Service用户添加到COM Services选项卡中的COM Security选项卡中,确保具有执行COM组件的权限。
As explained here . 这里所解释。

What I'm asking is, if you guys have any idea for understanding what is wrong with the Open() method. 我要问的是,你们是否有任何想法了解Open()方法的问题。

I would look at using this to read an excel file 我会看着用它来读取Excel文件

http://exceldatareader.codeplex.com/ http://exceldatareader.codeplex.com/

Then this it will allow you to print to PDF. 然后,这将使您可以打印为PDF。

http://pdfsharp.codeplex.com/ http://pdfsharp.codeplex.com/

its open source and free to use in commercial applications. 它是开源的,可在商业应用中免费使用。

Not a complete solution but its a start. 不是一个完整的解决方案,而是一个开始。 Then your not using Com in a server environment 然后,您不在服务器环境中使用Com

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Microsoft.Office.Interop.Excel或EPPlus,用于读取巨大(或不巨大)的Excel文件 - Microsoft.Office.Interop.Excel or EPPlus for read a huge (or not) Excel file Office 2007的Microsoft.Office.Interop.Excel - Microsoft.Office.Interop.Excel for Office 2007 无法使用 Microsoft.Office.Interop.Excel 加载文件或程序集“office,版本 = 15.0.0.0” - Could not load file or assembly ‘office, Version=15.0.0.0 using Microsoft.Office.Interop.Excel 使用Microsoft.Office.Interop.Excel导出Excel时出错 - Error when export Excel using Microsoft.Office.Interop.Excel 使用 Microsoft.Office.Interop.Excel 的问题:无法加载文件或程序集 'office,版本 = 15.0.0.0 - Issue using Microsoft.Office.Interop.Excel: Could not load file or assembly 'office, Version=15.0.0.0 使用Excel = Microsoft.Office.Interop.Excel编译错误 - using Excel = Microsoft.Office.Interop.Excel Compilation Error Microsoft.Office.Interop.Excel-* .csv文件打开 - Microsoft.Office.Interop.Excel - *.csv file opening 无法加载文件或程序集“ Microsoft.Office.Interop.Excel” - Could not load file or assembly 'Microsoft.Office.Interop.Excel' 如何使用Microsoft.Office.Interop.Excel阅读Excel文件中的特定工作表 - How to read a specific sheet in an Excel file using Microsoft.Office.Interop.Excel 应用程序使用 Microsoft.Office.Interop.Excel 打开 Excel 文件与服务器上的 RDP 会话冲突 - Application using Microsoft.Office.Interop.Excel to open excel file conflict with RDP sessions on the server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM