kilya

LibrarieClass

May 20th, 2020
1,493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.46 KB | None | 0 0
  1. using Microsoft.Office.Interop.Excel;
  2. using System;
  3. using System.IO;
  4.  
  5. namespace MultipleExcelToPDF
  6. {
  7.     public class LibrarieClass
  8.     {
  9.         private static Application excelApplication = null;
  10.         private static Workbook excelWorkBook = null;
  11.         private static object paramMissing = Type.Missing;
  12.  
  13.         public static int SearchDirectoryTree(string path, out string[] XLSfiles)
  14.         {
  15.             XLSfiles = Directory.GetFiles(path, "*.xls", SearchOption.AllDirectories);
  16.             return XLSfiles.Length;
  17.         }
  18.  
  19.         public static void ProcessFiles(string[] XLSfiles)
  20.         {
  21.             excelApplication = new Application();
  22.             foreach (string filePath in XLSfiles)
  23.             {
  24.  
  25.                 string paramSourceBookPath = filePath;
  26.                 // Get Extension of filePath
  27.                 string extension = Path.GetExtension(filePath);
  28.                 string paramExportFilePath = filePath.Replace(extension, ".pdf"); // Replace Extension .xls or .xlsx
  29.                 XlFixedFormatType paramExportFormat = XlFixedFormatType.xlTypePDF;
  30.                 XlFixedFormatQuality paramExportQuality = XlFixedFormatQuality.xlQualityStandard;
  31.                 bool paramOpenAfterPublish = false; // not open the pdf file after publish
  32.                 bool paramIncludeDocProps = true;
  33.                 bool paramIgnorePrintAreas = true;
  34.                 object paramFromPage = Type.Missing;
  35.                 object paramToPage = Type.Missing;
  36.  
  37.                 // Open the source workbook
  38.                 excelWorkBook = excelApplication.Workbooks.Open(paramSourceBookPath,
  39.                         paramMissing, paramMissing, paramMissing, paramMissing,
  40.                         paramMissing, paramMissing, paramMissing, paramMissing,
  41.                         paramMissing, paramMissing, paramMissing, paramMissing,
  42.                         paramMissing, paramMissing);
  43.  
  44.                 // Save it in the target format
  45.                 if (excelWorkBook != null)
  46.                     excelWorkBook.ExportAsFixedFormat(paramExportFormat,
  47.                         paramExportFilePath, paramExportQuality,
  48.                         paramIncludeDocProps, paramIgnorePrintAreas, paramFromPage,
  49.                         paramToPage, paramOpenAfterPublish,
  50.                         paramMissing);
  51.                 CloseWorkBook();
  52.             }
  53.             QuitExcel();
  54.         }
  55.  
  56.         public static void CloseWorkBook()
  57.         {
  58.             // Close the workbook object
  59.             if (excelWorkBook != null)
  60.             {
  61.                 excelWorkBook.Close(false, paramMissing, paramMissing);
  62.                 excelWorkBook = null;
  63.             }
  64.         }
  65.  
  66.         public static void QuitExcel()
  67.         {
  68.             // Quit Excel and release the ApplicationClass object
  69.             if (excelApplication != null)
  70.             {
  71.                 excelApplication.Quit();
  72.                 excelApplication = null;
  73.             }
  74.  
  75.             GC.Collect();
  76.             GC.WaitForPendingFinalizers();
  77.         }
  78.  
  79.         public static int ContextualCall(string folderPath)
  80.         {
  81.             int count = 0;
  82.             try
  83.             {
  84.                 count = SearchDirectoryTree(folderPath, out string[] XLSFiles);
  85.                 ProcessFiles(XLSFiles);
  86.             }
  87.             catch
  88.             {
  89.                 CloseWorkBook();
  90.                 QuitExcel();
  91.             }
  92.             return count;
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment