public void ConvertToPdf(IEnumerable <string> excelFilesPathToConvert) { using (var excelApplication = new ExcelApplicationWrapper()) { foreach (var excelFilePath in excelFilesPathToConvert) { var thisFileWorkbook = excelApplication.ExcelApplication.Workbooks.Open(excelFilePath); string newPdfFilePath = Path.Combine( Path.GetDirectoryName(excelFilePath), $"{Path.GetFileNameWithoutExtension(excelFilePath)}.pdf"); thisFileWorkbook.ExportAsFixedFormat( Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, newPdfFilePath); thisFileWorkbook.Close(false, excelFilePath); Marshal.ReleaseComObject(thisFileWorkbook); } } }
public void ConvertToPdf(IEnumerable <string> excelFilesPathToConvert, string dest) { using (var excelApplication = new ExcelApplicationWrapper()) { foreach (var excelFilePath in excelFilesPathToConvert) { Log.Write($"Started Convesion of file : {excelFilePath}"); var thisFileWorkbook = excelApplication.ExcelApplication.Workbooks.Open(excelFilePath); string newPdfFilePath = Path.Combine( (dest.Length == 0 ? Path.GetDirectoryName(excelFilePath):dest), $"{Path.GetFileNameWithoutExtension(excelFilePath)}.pdf"); thisFileWorkbook.ExportAsFixedFormat( Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, newPdfFilePath); Log.Write($"PDF saved to : {newPdfFilePath}"); thisFileWorkbook.Close(false, excelFilePath); Marshal.ReleaseComObject(thisFileWorkbook); Log.Write($"Completed Convesion of file : {excelFilePath}"); } } }