转换后的Excel到PDF无法打开该文件 [英] After converting Excel to PDF Unable to open that file

查看:241
本文介绍了转换后的Excel到PDF无法打开该文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图 Excel中转换为 PDF ,我可以把它转换。但在打开该文件,我得到错误的图像下面。

I have tried to convert excel to PDFand I could convert it. but while opening that file I am getting error as following in image.

我有请参阅本链接怎样的Word文件转换为PDF编程?从这个链接,我可以解决我的Word 的以PDF文件的转换问题。在这里我没有对服务器和同样的事情安装办公室我要为 Excel中 PPT 文件做。

I have refer this link How do I convert Word files to PDF programmatically? from this link I could solve my Word to PDF file conversion issue. where I don't have to install office on server and same thing I want to do for excel and PPT files.

错误而PDF打开

我的code:

// Create a new Microsoft Excel application object                
Microsoft.Office.Interop.Excel.Application excelApplication = new Microsoft.Office.Interop.Excel.Application();

// C# doesn't have optional arguments so we'll need a dummy value
object oMissing = System.Reflection.Missing.Value;

excelApplication.Visible = false;
excelApplication.ScreenUpdating = false;

FileInfo ExcelFile = new FileInfo(sourcePath);

// Cast as Object for Excel Open method
Object filename = (Object)ExcelFile.FullName;

// Use the dummy value as a placeholder for optional arguments                

Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApplication.Workbooks.Open(ExcelFile.FullName, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                                    Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                                    Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                                    Type.Missing, Type.Missing);


object outputFileName = ExcelFile.FullName.Replace(Path.GetFileName(ExcelFile.FullName), Path.GetFileName(targetPath));
object fileFormat = WdSaveFormat.wdFormatPDF;

// Save document into PDF Format
//excelWorkbook.SaveAs(outputFileName);
excelWorkbook.SaveAs(outputFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, true, false, XlSaveAsAccessMode.xlNoChange, XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);                
// Close the Excel Workbook, but leave the Word application open.
// excelWorkbook has to be cast to type _Document so that it will find the
// correct Close method.                
object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
((Microsoft.Office.Interop.Excel.Workbook)excelWorkbook).Close();
excelWorkbook = null;

// Excel has to be cast to type _Application so that it will find
// the correct Quit method.
((Microsoft.Office.Interop.Excel._Application)excelApplication).Quit();
excelWorkbook = null;

注意

我不想使用内置出口功能来做到这一点。如下我已经有code为it.This正在code转换的擅长为PDF

I don't want to use inbuilt export functionality to accomplish this. as follow i have already have code for it.This is working code for converting excel to pdf.

Microsoft.Office.Interop.Excel.Application excelApplication = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = null;
excelWorkbook = excelApplication.Workbooks.Open(sourcePath);
excelWorkbook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, targetPath);
excelWorkbook.Close();
excelApplication.Quit();
excelApplication = null;

请让我知道我所做的错误。

Please let me know where I am doing mistake.

推荐答案

在code正在工作。只是检查是否要传递正确的文件名(含延伸)参数源和目标。另外,请检查您安装的Office是否另存为PDF选项

The code is working. Just check whether you are passing correct file name (with extention) parameter for source and target. Also check whether your installed office have save as PDF option

     // Create a new Microsoft Word application object
        Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();

        // C# doesn't have optional arguments so we'll need a dummy value
        object oMissing = System.Reflection.Missing.Value;

        word.Visible = false;
        word.ScreenUpdating = false;

        FileInfo wordFile = new FileInfo("c:\\a.docx");

        // Cast as Object for word Open method
        Object filename = (Object)wordFile.FullName;

        // Use the dummy value as a placeholder for optional arguments
        Microsoft.Office.Interop.Word.Document doc = word.Documents.Open(ref filename, ref oMissing,
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing);
        doc.Activate();

        object outputFileName = wordFile.FullName.Replace(System.IO.Path.GetFileName(wordFile.FullName), System.IO.Path.GetFileName("c:\\a.pdf"));
        object fileFormat = WdSaveFormat.wdFormatPDF;

        // Save document into PDF Format
        doc.SaveAs(ref outputFileName,
                            ref fileFormat, ref oMissing, ref oMissing,
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing);

        // Close the Word document, but leave the Word application open.
        // doc has to be cast to type _Document so that it will find the
        // correct Close method.                
        object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
        ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
        doc = null;

        // word has to be cast to type _Application so that it will find
        // the correct Quit method.
        ((Microsoft.Office.Interop.Word._Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
        word = null;

这篇关于转换后的Excel到PDF无法打开该文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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