【问题标题】:After converting Excel to PDF Unable to open that file将 Excel 转换为 PDF 后无法打开该文件
【发布时间】:2016-04-05 08:08:19
【问题描述】:

我尝试将excel 转换为PDF,我可以转换它。但是在打开该文件时,出现如下错误。

我已经从这个链接中引用了这个链接How do I convert Word files to PDF programmatically?,我可以解决我的 Word 到 PDF 文件转换问题。我不必在服务器上安装 Office 并且我想为 excelPPT 文件做同样的事情。

我的代码:

// 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;

注意

我不想使用内置的导出功能来完成此操作。如下我已经有它的代码。这是将excel转换为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;

请让我知道我在哪里做错了。

【问题讨论】:

    标签: c# .net excel pdf asp.net-mvc-5


    【解决方案1】:

    试试这个:-这个代码足以将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();
    

    请在该方法之外创建:

    public Microsoft.Office.Interop.Excel.Workbook excelDocument { get; set; }
    

    【讨论】:

      【解决方案2】:

      代码正在运行。只需检查您是否为源和目标传递了正确的文件名(带扩展名)参数。还要检查您安装的办公室是否有另存为 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;
      

      【讨论】:

      • 请阅读完整问题我面临的问题是 excel 到 PDF 的转换而不是 word,我已经知道我可以成功地将 word 转换为 PDF。
      • 是的,这是错误的问题,你有什么解决我的问题的办法吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-07
      • 1970-01-01
      • 2012-11-30
      • 2014-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多