private void SaveWorkBook(Excel.Workbook wb, string filename)
        {
            string extension = System.IO.Path.GetExtension(filename);

            if (extension.Equals(".xlsx"))
            {
                wb.SaveAs(filename, missing,
                          missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlShared,
                          Excel.XlSaveConflictResolution.xlLocalSessionChanges, missing, missing, missing, missing);
            }
            else if (extension.Equals(".pdf"))
            {
                string paramExportFilePath = filename;
                Excel.XlFixedFormatType    paramExportFormat  = Excel.XlFixedFormatType.xlTypePDF;
                Excel.XlFixedFormatQuality paramExportQuality =
                    Excel.XlFixedFormatQuality.xlQualityStandard;
                bool   paramOpenAfterPublish = false;
                bool   paramIncludeDocProps  = true;
                bool   paramIgnorePrintAreas = true;
                object paramFromPage         = Type.Missing;
                object paramToPage           = Type.Missing;
                // Save it in the target format.
                wb.ExportAsFixedFormat(paramExportFormat,
                                       paramExportFilePath, paramExportQuality,
                                       paramIncludeDocProps, paramIgnorePrintAreas, paramFromPage,
                                       paramToPage, paramOpenAfterPublish,
                                       missing);
            }
        }
        public void ConvertExcelToPdf(string excelFileIn, string pdfFileOut)
        {
            CheckForExistingExcellProcesses();

            var missing = Type.Missing;//System.Reflection.Missing.Value;

            Application excel = new Application();

            Microsoft.Office.Interop.Excel.Workbook wbk = null;

            GC.GetTotalMemory(false);
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.GetTotalMemory(true);

            try
            {
                excel.Visible        = false;
                excel.ScreenUpdating = false;
                excel.DisplayAlerts  = false;

                FileInfo excelFile = new FileInfo(excelFileIn);

                string filename = excelFile.FullName;

                GetTheExcelProcessIdThatUsedByThisInstance();

                wbk = excel.Workbooks.Open(filename, missing,
                                           missing, missing, missing, missing, missing,
                                           missing, missing, missing, missing, missing,
                                           missing, missing, missing);

                wbk.Activate();

                object outputFileName = pdfFileOut;

                Microsoft.Office.Interop.Excel.XlFixedFormatType    fileFormat         = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;
                Microsoft.Office.Interop.Excel.XlFixedFormatQuality paramExportQuality = Microsoft.Office.Interop.Excel.XlFixedFormatQuality.xlQualityStandard;
                bool paramIncludeDocProps  = true;
                bool paramIgnorePrintAreas = false;

                if (wbk != null)//save as pdf

                {
                    wbk.ExportAsFixedFormat(fileFormat,
                                            outputFileName,
                                            paramExportQuality,
                                            paramIncludeDocProps,
                                            paramIgnorePrintAreas,
                                            missing,
                                            missing,
                                            missing,
                                            missing);
                }

                object saveChanges = XlSaveAction.xlDoNotSaveChanges;
                ((_Workbook)wbk).Close(saveChanges, missing, missing);
                excel.Quit();

                // liberar
                releaseObject(wbk);
                releaseObject(excel);

                wbk   = null;
                excel = null;

                GC.GetTotalMemory(false);
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.GetTotalMemory(true);
            }
            catch (Exception ex)
            {
                // Cerrar
                ((_Workbook)wbk).Close(false, missing, missing);
                excel.Quit();

                // Elimina el archivo creado
                File.Delete(pdfFileOut);

                KillExcelProcessThatUsedByThisInstance();

                throw (ex);
            }
            finally
            {
                // Liberar
                releaseObject(wbk);
                releaseObject(excel);

                wbk   = null;
                excel = null;

                GC.GetTotalMemory(false);
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.GetTotalMemory(true);

                KillExcelProcessThatUsedByThisInstance();
            }
        }