Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1.         public void SaveFile()
  2.         {
  3.             if (Data.Count > 0)
  4.             {
  5.                 var excelApp = new Excel.Application();
  6.  
  7.                 excelApp.Visible = false;
  8.  
  9.                 excelApp.DisplayAlerts = false;
  10.  
  11.                 excelApp.Workbooks.Add();
  12.  
  13.                 Excel._Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet;
  14.  
  15.                 GenerateHeaderLine(workSheet);
  16.  
  17.                 int line = 2;
  18.  
  19.                 foreach (T item in Data)
  20.                 {
  21.                     for (int i = 0, j = Headers.Count; i < j; i++)
  22.                     {
  23.                         object value = item.GetType().GetProperty(Headers[i]).GetValue(item, null);
  24.  
  25.                         if (value == null)
  26.                         {
  27.                             value = "";
  28.                         }
  29.  
  30.                         int s = 65 + i;
  31.  
  32.                         char c = (char)s;
  33.  
  34.                         workSheet.Cells[line, c.ToString()] = value.ToString();
  35.                     }
  36.  
  37.                     line++;
  38.                 }
  39.  
  40.                 string fullPath = Path.Combine(Dir, FileName);
  41.  
  42.                 if (File.Exists(fullPath))
  43.                 {
  44.                     File.Delete(fullPath);
  45.                 }
  46.  
  47.                 excelApp.ActiveWorkbook.SaveCopyAs(fullPath);
  48.  
  49.                 excelApp.ActiveWorkbook.Close();
  50.  
  51.                 excelApp.Quit();
  52.  
  53.                 excelApp = null;
  54.  
  55.                 System.Diagnostics.Process.GetProcessesByName("EXCEL").ToList().ForEach((x) =>
  56.                 {
  57.                     x.Kill();
  58.                 });
  59.             }
  60.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement