[C #] Excel operation of the same name into the same folder in Excel, and then combined into a single Excel file

Recently there is a demand for Excel operations, as are the duplication of work, so as to share the code, and I was not considered a rookie technical performance, if there is a large cattle, all the better able to advise

Advance computer need to install Excel, and then Vs cited Microsoft.Office.Interop.Excel;

Before using the code below, please add using Excel = Microsoft.Office.Interop.Excel;

 

 

Excel with the same name into the same folder

Premise: the format of the same type Excel file name is: A (1), A ( 2) of this.

Packet using regular expressions, as follows:

. 1  ///  <Summary> 
2          /// the same name Excel packets
 . 3          ///  </ Summary> 
. 4          void the MoveFile ()
 . 5          {
 . 6              // to your own path 
. 7              String path = @ " C: \ the Users \ Reasonable \ Desktop \ Table " ;
 . 8              the DirectoryInfo the root = new new the DirectoryInfo (path);
 . 9              the FileInfo [] = Files root.GetFiles ();
 10  
. 11              the Regex fileRegex = new new the Regex ( @" .? + (= \ () " );
 12 
13             foreach (var file in files)
14             {
15                 var fileName = fileRegex.Match(file.Name).ToString();
16 
17                 var targetPath = $@"{path}\{fileName}";
18 
19                 //以文件名创建子目录
20                 if (!Directory.Exists(targetPath))
21                 {
22                     Directory.CreateDirectory(targetPath);
23                 }
24 
25                 file.MoveTo(Path.Combine(targetPath, file.Name));
26             }
27 
28             Console.Read();
29         }
30     }
View Code

 

The same folder merged into a single Excel

Premise: As the advance implementation of the above grouping code, so there is no file in the root directory, and if there is a demand, please modify your own.

 Initially I used Excel macro operation, but one needs to run, too cumbersome, so instead use C # or automatically run.

. 1  ///  <Summary> 
2          /// plurality excel combined
 . 3          ///  </ Summary> 
. 4          void MergeExcel ()
 . 5          {
 . 6              // to your own path 
. 7              String path = @ " C: \ the Users \ Reasonable \ Desktop \ Table " ;
 . 8              the DirectoryInfo the root = new new the DirectoryInfo (path);
 . 9              // get all subdirectories 
10              var dires = root.GetDirectories ();
 . 11              the foreach ( var the dir in dires)
 12 is             {
13                 Excel.Application app = new Excel.Application();
14                 Excel._Workbook result = app.Workbooks.Add();
15                 //获取子目录的文件
16                 FileInfo[] files = dir.GetFiles();
17 
18                 foreach (var file in files)
19                 {
20                     Excel._Workbook wb1 = app.Workbooks.Open(Path.GetFullPath(file.FullName));
21                     foreach (Excel._Worksheet each in wb1.Sheets)
22                     {
23                         each.Copy(result.Worksheets[1]);
24                     }
25                     wb1.Close();
26                 }
27                 result.SaveAs($@"{dir.FullName}\new.xlsx");
28                 app.Quit();
29             }
30         }
View Code

 

Guess you like

Origin www.cnblogs.com/gdvxfgv/p/10954085.html