Nilesh Patil

Nilesh Patil

  • 571
  • 2.9k
  • 44.1k

How to convert Entire Excel Workbook into PDf in C#

Mar 24 2018 8:28 AM
Hi All,
 
My below code is working fine for convert excel document to PDF but its not Convert Entire Excel Doc Some Large excel Content Are cut 
 
Please help me this is my code
 
public short excel2Pdf(string originalXlsPath, string pdfPath)
{
Console.WriteLine("Class: " + GetType() + " Method: " + MethodBase.GetCurrentMethod().Name + " Started ");
short convertExcel2PdfResult = -1;
// Create COM Objects
Microsoft.Office.Interop.Excel.Application excelApplication = null;
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = null;
object unknownType = Type.Missing;
// Create new instance of Excel
try
{
//open excel application
excelApplication = new Microsoft.Office.Interop.Excel.Application
{
ScreenUpdating = false,
DisplayAlerts = false
};
//open excel sheet
if (excelApplication != null)
excelWorkbook = excelApplication.Workbooks.Open(originalXlsPath, unknownType, unknownType,
unknownType, unknownType, unknownType,
unknownType, unknownType, unknownType,
unknownType, unknownType, unknownType,
unknownType, unknownType, unknownType);
if (excelWorkbook != null)
{
// Call Excel's native export function (valid in Office 2007 and Office 2010, AFAIK)
excelWorkbook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF,
pdfPath,
unknownType, unknownType, unknownType, unknownType, unknownType,
unknownType, unknownType);
convertExcel2PdfResult = 0;
}
else
{
Console.WriteLine("Error occured for conversion of office excel to PDF ");
convertExcel2PdfResult = 504;
}
}
catch (Exception exExcel2Pdf)
{
Console.WriteLine("Error occured for conversion of office excel to PDF, Exception: ", exExcel2Pdf);
convertExcel2PdfResult = 504;
}
finally
{
// Close the workbook, quit the Excel, and clean up regardless of the results...
if (excelWorkbook != null)
excelWorkbook.Close(unknownType, unknownType, unknownType);
if (excelApplication != null) excelApplication.Quit();
Util.releaseObject(excelWorkbook);
Util.releaseObject(excelApplication);
}
Console.WriteLine("Class: " + GetType() + " Method: " + MethodBase.GetCurrentMethod().Name + " Ended ");
return convertExcel2PdfResult;
}

Answers (5)