I have tried to convert excel
to PDF
and 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.
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.