Office 2007的Microsoft.Office.Interop.Excel

弹药

我想尝试将Office文件(Excel,Word,Powerpoint)转换为PDF文件的Windows窗体应用程序。我的客户的PC不会安装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;
        }
cremor

正如评论中已经说过的,每个客户端都需要安装2007 Microsoft Office加载项:Microsoft另存为PDF无论您使用的是Windows 8.1还是已安装任何PDF阅读器,都没有关系。您需要使用该加载项才能在Office 2007中编写PDF。(在Office 2010或2013中不需要任何加载项。)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Microsoft.Office.Interop.Excel的阅读范围

C#-Microsoft.Office.Interop.Excel.Sheets到Microsoft.Office.Interop.Excel.Worksheet []

是否有Microsoft.Office.Interop.Excel的替代库?

Build Server中的Microsoft.Office.Interop.Excel参考

使用Microsoft.Office.Interop Word和Excel

C#使用Microsoft.Office.Interop.Excel读取数据

Microsoft.Office.Interop.Excel未注册DLL

Microsoft.Office.Interop.Excel读取错误的颜色?

无法添加对microsoft.office.interop.excel的引用

无法将类型为“ microsoft.Office.Interop.Excel.ApplicationClass”的COM对象转换为“ microsoft.Office.Interop.Excel.Application”

我可以使用Microsoft.Office.Interop.Excel在未安装Microsoft Office Pack的系统上创建Excel文件吗?

没有安装Excel的情况下使用Microsoft.Office.Interop.Excel创建Excel文件

Microsoft.Office.Interop.Word OMath

使用Microsoft.office.Interop.Excel读取Excel时性能降低

Microsoft.Office.Interop.Excel.Application - 調用不同版本的 Excel

C#阅读使用的Microsoft.Office.Interop.Excel Excel单元格的值

使用Microsoft Office Interop Excel使用C#在excel图表中自定义数据标签

Microsoft.Office.Interop.Excel-使用分号分隔符打开CSV

我是否需要安装办公室才能使用Microsoft.Office.Interop.Excel.dll

无法转换类型为'Microsoft.Office.Interop.Excel.WorksheetClass'的COM对象

'Microsoft.Office.Interop.Excel.Range'不包含'get_Default'的定义

为什么Microsoft.Office.Interop.Excel.Application.Quit()使后台进程运行?

服务器上是否需要 MS Office 才能使用 Microsoft.Excel.interop

如何在ASP.NET中保存Microsoft.Office.Interop.Excel.Workbook

使用Microsoft.Office.Interop.Excel的C#从其他WorkSheet添加新系列和范围

无法在Windows XP中加载文件或程序集microsoft.office.interop.excel版本15.0.0.0

为什么在项目中引用Microsoft.Office.Interop.Excel.dll时会添加其他文件?

Microsoft.Office.Interop.Excel添加单元格非常慢

System.Runtime.InteropServices.InvalidComObjectException:尝试使用 Microsoft.Office.Interop.Excel 写入 Excel 单元格时