Skip to content

Window Application

Piyush Parikh

Asked Sep 24, 2013, 5:40 AM EDT1 answer
Share
LinkedInX
How to Convert xls to pdf and Docs to pdf in Window APplication in C#

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    If you're using Office 2007 or above then you can use the following code. 

    Convert xls to PDF.

    C#
    using System;
    using System.IO;
    using msExcel = Microsoft.Office.Interop.Excel;
    
    
    namespace demo {
    
    
        public class ExcelToPdfConverter {
    
    
            private static object missing = System.Reflection.Missing.Value;
    
    
            public static void ConvertExcelToPdf(string excelFileIn, string pdfFileOut) {
                msExcel.Application excel = new msExcel.Application();
                try {
                    excel.Visible = false;
                    excel.ScreenUpdating = false;
                    excel.DisplayAlerts = false;
    
    
                    FileInfo excelFile = new FileInfo(excelFileIn);
    
    
                    string filename = excelFile.FullName;

                    msExcel.Workbook wbk = excel.Workbooks.Open(filename, missing,
                        missing, missing, missing, missing, missing,
                        missing, missing, missing, missing, missing,
    Code
                        missing, missing, missing);
                    wbk.Activate();
    
    
                    object outputFileName = pdfFileOut;
                    msExcel.XlFixedFormatType fileFormat = msExcel.XlFixedFormatType.xlTypePDF;
    
    
                    // Save document into PDF Format
                    wbk.ExportAsFixedFormat(fileFormat, outputFileName,
                        missing, missing, missing,
                        missing, missing, missing,
    Code
                        missing);
    
    
                    object saveChanges = msExcel.XlSaveAction.xlDoNotSaveChanges;
                    ((msExcel._Workbook)wbk).Close(saveChanges, missing, missing);
                    wbk = null;
                }
                finally {
                    ((msExcel._Application)excel).Quit();
                    excel = null;
                }
            }
    
    
        }
    0