Congratulations - C# Corner Q4, 2022 MVPs Announced
Why Join
Become a member
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
C# Corner Home
Technologies
Monthly Leaders
ASK A QUESTION
Forum guidelines
Nilesh Patil
682
3k
100.3k
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;
}
Reply
Answers (
5
)
Selection CHANGED NOT WORK
Datetimepicker selection changed