C# 将Excel转换为PDF后,无法打开该文件

C# 将Excel转换为PDF后,无法打开该文件,c#,.net,excel,pdf,asp.net-mvc-5,C#,.net,Excel,Pdf,Asp.net Mvc 5,我曾尝试将excel转换为PDF,我可以将其转换。但当打开那个文件时,我得到了如下图中的错误 Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); excelDocument = excel.Workbooks.Open(savedFileName, ReadOnly: true); excelDocument.ExportAsFixedFormat

我曾尝试将excel转换为PDF,我可以将其转换。但当打开那个文件时,我得到了如下图中的错误

Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excelDocument = excel.Workbooks.Open(savedFileName, ReadOnly: true);
excelDocument.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, attahcmentPath + "/pdf" + attachment.BetAttachmentCode + ".pdf");

excelDocument.Close();
excel.Quit();
我已经从这个链接引用了这个链接,我可以解决我的Word到PDF文件转换问题。我不需要在服务器上安装office,也不需要在excel和PPT文件上安装office

Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excelDocument = excel.Workbooks.Open(savedFileName, ReadOnly: true);
excelDocument.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, attahcmentPath + "/pdf" + attachment.BetAttachmentCode + ".pdf");

excelDocument.Close();
excel.Quit();
我的代码:

Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excelDocument = excel.Workbooks.Open(savedFileName, ReadOnly: true);
excelDocument.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, attahcmentPath + "/pdf" + attachment.BetAttachmentCode + ".pdf");

excelDocument.Close();
excel.Quit();

Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excelDocument = excel.Workbooks.Open(savedFileName, ReadOnly: true);
excelDocument.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, attahcmentPath + "/pdf" + attachment.BetAttachmentCode + ".pdf");

excelDocument.Close();
excel.Quit();
我不想使用内置的导出功能来实现这一点。下面我已经有了它的代码。这是将excel转换为pdf的工作代码

Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excelDocument = excel.Workbooks.Open(savedFileName, ReadOnly: true);
excelDocument.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, attahcmentPath + "/pdf" + attachment.BetAttachmentCode + ".pdf");

excelDocument.Close();
excel.Quit();

请告诉我哪里出错。

代码正在运行。只需检查是否为源和目标传递了正确的文件名和扩展参数。还要检查您安装的office是否具有另存为PDF选项

     // 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;
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excelDocument = excel.Workbooks.Open(savedFileName, ReadOnly: true);
excelDocument.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, attahcmentPath + "/pdf" + attachment.BetAttachmentCode + ".pdf");

excelDocument.Close();
excel.Quit();

试试这个:-这段代码足以将excel转换为pdf。 为我工作,我可以阅读pdf文件

Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excelDocument = excel.Workbooks.Open(savedFileName, ReadOnly: true);
excelDocument.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, attahcmentPath + "/pdf" + attachment.BetAttachmentCode + ".pdf");

excelDocument.Close();
excel.Quit();
在该方法之外,请创建:

Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excelDocument = excel.Workbooks.Open(savedFileName, ReadOnly: true);
excelDocument.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, attahcmentPath + "/pdf" + attachment.BetAttachmentCode + ".pdf");

excelDocument.Close();
excel.Quit();
public Microsoft.Office.Interop.Excel.Workbook excelDocument { get; set; }

请阅读完整问题我面临的问题是excel到PDF的转换,而不是word,我已经知道我可以成功地将word转换为PDF。是的,这是一个错误的问题,你有任何解决我的问题的方法吗。