Merge multiple Excel column Value to One Consolidate Excel

In this code snippet, I write the code for consolidating the multiple excel value in single excel common column wise.
For example, we maintain the company wise daily report excel and end of the week we want to merge company excel to one excel that shows company daily report day wise.
  1. string FilesPath = @"D:\Files";  
  2. //Get the all Files from the specific Path  
  3. DirectoryInfo dirDocument = new DirectoryInfo(FilesPath);  
  4. var files = dirDocument.GetFiles();  
  5. DataSet ds = new DataSet();  
  6. // Get array of Files  
  7. FileInfo[] filesD = dirDocument.GetFiles();  
  8. // Loop for files  
  9. foreach (FileInfo fileD in filesD)  
  10. {  
  11. string ExtensionC = fileD.Extension;  
  12. string FilePathC = txtFolderPath.Text;  
  13. string FileNameC = fileD.Name;  
  14. //Condition for Check and get the main Consolidate File  
  15. if (FileNameC.ToLower().Contains("SpecificMainExcel"))  
  16. {  
  17. //code for Main Consolidate excel initialise and opens an Excel workbook.  
  18. Excel.Application xlAppC = new Excel.Application();  
  19. Excel.Workbook xlWorkbookC = xlAppC.Workbooks.Open(@"" + FilesPath + "/" + FileNameC);  
  20. Excel._Worksheet xlWorksheetC = xlWorkbookC.Sheets[1];  
  21. Excel.Range xlRangeC = xlWorksheetC.UsedRange;  
  22. xlAppC.DisplayAlerts = false;  
  23. //xlAppC.ScreenUpdating = false;  
  24. //xlAppC.Visible = false;  
  25. // Get the count of Rows and Columns  
  26. int rowCountC = xlRangeC.Rows.Count;  
  27. int colCountC = xlRangeC.Columns.Count;  
  28. int count = 0;  
  29. // Loop for get All files and check and match columns with Consolidate excel sheet columns.  
  30. foreach (FileInfo file in files)  
  31. {  
  32. string Extension = file.Extension;  
  33. string FilePath = txtFolderPath.Text;  
  34. string FileName = file.Name;  
  35. int Start, End;  
  36. Start = FileName.IndexOf('-', 0);  
  37. End = FileName.Length;  
  38. //Get Excel Actual File Name  
  39. string ActualFileName = FileName.Substring(Start + 2, End - 2 - Start);  
  40. switch (Extension)  
  41. {  
  42. case ".xls"//Excel 97-03  
  43. ActualFileName = ActualFileName.Remove(ActualFileName.Length - 4);  
  44. break;  
  45. case ".xlsx"//Excel 07  
  46. ActualFileName = ActualFileName.Remove(ActualFileName.Length - 5);  
  47. break;  
  48. }  
  49. //code for initialise and opens an Excel workbook.  
  50. Excel.Application xlApp = new Excel.Application();  
  51. Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"" + FilesPath + "/" + FileName);  
  52. Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];  
  53. Excel.Range xlRange = xlWorksheet.UsedRange;  
  54. int rowCount = xlRange.Rows.Count;  
  55. int colCount = xlRange.Columns.Count;  
  56. // Loop for columns in file  
  57. for (int col = 1; col <= colCountC; col++)  
  58. {  
  59. string filedec = Convert.ToString(xlRangeC.Cells[1, col].Value2);  
  60. if (filedec != null)  
  61. {  
  62. // Match the columns name  
  63. if (filedec.ToLower().Contains(ActualFileName.ToLower()))  
  64. {  
  65. //for (int row = 2; row <= rowCountC; row++)  
  66. //{  
  67. for (int i = 2; i <= rowCount; i++)  
  68. {  
  69. // Assign the value file to consolidate file column  
  70. xlRangeC.Cells[i, col] = Convert.ToString(xlRange.Cells[i, 6].Value2);  
  71. }  
  72. // }  
  73. }  
  74. }  
  75. }  
  76. // Close the worksheet  
  77. xlWorkbook.Close();  
  78. System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlApp);  
  79. System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlWorkbook);  
  80. System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlWorksheet);  
  81. }  
  82. // Close the worksheet  
  83. xlWorkbookC.Close(true, Type.Missing, Type.Missing);  
  84. xlAppC.Quit();  
  85. System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlAppC);  
  86. System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlWorkbookC);  
  87. System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlWorksheetC);  
  88. }  
  89. }  
  90. MessageBox.Show("Records insert successfully !""Done");   
If any query please comments below.