0

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.

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.

Error while pdf open

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

Note

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.

Community
  • 1
  • 1
Govinda Rajbhar
  • 2,926
  • 6
  • 37
  • 62

2 Answers2

0

Try this one:-this code is enough to convert excel to pdf. work for me and i can read pdf file.

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();

outside the that method please create:

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

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;