使用Microsoft.Office.Interop.Excel将文件保存为PDF [英] Saving file as PDF by using Microsoft.Office.Interop.Excel

查看:1791
本文介绍了使用Microsoft.Office.Interop.Excel将文件保存为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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()使方法执行然后sopped甚至不执行..(W / O任何异常或任何错误,我可以知道,甚至不是在我的日志文件或在事件查看器..),最终没有.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.

以下是我的代码:

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.

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

如解释此处

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.
As explained here.

我要求的是,如果你们有任何想法,

What I'm asking is, if you guys have any idea for understanding what is wrong with the Open() method.

推荐答案

我会用这个来读取excel文件

I would look at using this to read an excel file

http://exceldatareader.codeplex.com/

然后这将允许您打印到PDF。

Then this it will allow you to print to PDF.

http://pdfsharp.codeplex.com/

其开放源代码并可在商业应用程序中使用。

its open source and free to use in commercial applications.

不是一个完整的解决方案, 。那么你不在服务器环境中使用Com

Not a complete solution but its a start. Then your not using Com in a server environment

这篇关于使用Microsoft.Office.Interop.Excel将文件保存为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆