C# Microsoft.Office.Interop.Excel for Office 2007

C# Microsoft.Office.Interop.Excel for Office 2007,c#,winforms,ms-office,office-interop,C#,Winforms,Ms Office,Office Interop,我想尝试将office文件Excel、Word、Powerpoint转换为PDF文件的Windows窗体应用程序。 我客户的电脑不会安装Visual Studio,Office版本为2007。 我的应用程序使用Microsoft.Office.Iterop.Excel.dll转换为PDF格式。 在我客户端的PC上找不到此dll文件,出现了如下错误 System.AugumentException: Value does not fall within the expected range. at

我想尝试将office文件Excel、Word、Powerpoint转换为PDF文件的Windows窗体应用程序。 我客户的电脑不会安装Visual Studio,Office版本为2007。 我的应用程序使用Microsoft.Office.Iterop.Excel.dll转换为PDF格式。 在我客户端的PC上找不到此dll文件,出现了如下错误

System.AugumentException: Value does not fall within the expected range.
at Microsoft.Office.Interop.Excel._Workbook.ExportAsFixedFromat(.......)
我怎样才能解决这个问题

我的代码如下

public bool ExportWorkbookToPdf(string workbookPath, string outputPath)
        {
            // If either required string is null or empty, stop and bail out
            if (string.IsNullOrEmpty(workbookPath) || string.IsNullOrEmpty(outputPath))
            {
                return false;
            }

            // Create COM Objects
            Microsoft.Office.Interop.Excel.Application excelApplication;
            Microsoft.Office.Interop.Excel.Workbook excelWorkbook;

            // Create new instance of Excel
            excelApplication = new Microsoft.Office.Interop.Excel.Application();

            // Make the process invisible to the user
            excelApplication.ScreenUpdating = false;

            // Make the process silent
            excelApplication.DisplayAlerts = false;

            // Open the workbook that you wish to export to PDF
            excelWorkbook = excelApplication.Workbooks.Open(workbookPath);

            MessageBox.Show(workbookPath);
            // If the workbook failed to open, stop, clean up, and bail out
            if (excelWorkbook == null)
            {
                excelApplication.Quit();

                excelApplication = null;
                excelWorkbook = null;
                MessageBox.Show("in null");
                return false;
            }

            var exportSuccessful = true;
            try
            {
                // Call Excel's native export function (valid in Office 2007 and Office 2010, AFAIK)
                excelWorkbook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, outputPath);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
                // Mark the export as failed for the return value...
                exportSuccessful = false;

                // Do something with any exceptions here, if you wish...
                // MessageBox.Show...        
            }
            finally
            {
                // Close the workbook, quit the Excel, and clean up regardless of the results...
                excelWorkbook.Close();
                excelApplication.Quit();

                excelApplication = null;
                excelWorkbook = null;
            }

            // You can use the following method to automatically open the PDF after export if you wish
            // Make sure that the file actually exists first...
            if (System.IO.File.Exists(outputPath))
            {
                MessageBox.Show(outputPath);
                System.Diagnostics.Process.Start(outputPath);
            }

            return exportSuccessful;
        }

如评论中所述,每个客户端都需要安装。无论您是在Windows 8.1上还是安装了任何PDF阅读器。使用Office2007编写PDF时需要该加载项。Office 2010或2013不需要任何外接程序。

Excel安装在计算机上,对吗?您可以共享出现此错误的代码吗?我在计算机上安装了Office 2007。错误发生在excelWorkbook.ExportAsFixedFormatMicrosoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF,outputPath;outputPath是否包含路径,或包含路径的文件名?包含路径的文件名。[例如,C:\\Test.pdf],很抱歉发布德语链接,但是:如果未安装pdf加载项,则会发生错误。也许是这样?我知道了,非常感谢。