コード例 #1
0
        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);
                }
            }
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: mdabrarsami/ExcelToPdf
        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}");
                }
            }
        }