There are multiple structured Excel, with complex headers that need to be merged into one, and the extra header data is removed, and the COM component can be used to read the range of each Excel table to merge into a new table. Sample such as Figure
There are many tables in the same format, and the merged code is as follows:
1.using System;
2.using System.Collections.Generic;
3.using System.Text;
4.using System.Reflection;
5.using Excel = Microsoft.Office.Interop.Excel; 6.namespace ConsoleApplication20 7. {8.//Add Reference-com-microsoft Excel 11.0 Object libery 9. Class Program 10. {one. static void Main (string [] args) 12. {//m is the table width label (the last column in Excel), and 3 is the header height 14. Mergeexcel.domerge (new string [] 15. {@ "E:\excel\ type A\ company A.xls", 17. @ "E:\excel\ type A\ company B.xls" 18. }, 19.
@ "E:\excel\ type a\ merge test. xls", "M", 3); Mergeexcel.domerge (new string [] 21. {d. @ "E:\excel\ type b\ tab A.xls", 23. @ "E:\excel\ Type b\ tab B.xls" 24. }, 25.
@ "E:\excel\ type b\ merge test. xls", "I", 4);
26.} 27.
28.29. 30.31. } 32. public class Mergeexcel 33. {34.35. Excel.Application app = new Microsoft.offiCe.
Interop.Excel.ApplicationClass (); 36.//Save target Object 37.
Excel.Workbook bookdest = null;
Excel.Worksheet sheetdest = null; 39.//Read the data Object 40.
Excel.Workbook booksource = null;
Excel.Worksheet sheetsource = null;
42.43.
A. String [] _sourcefiles = null; A. String _destfile = string.
Empty; A. String _columnend = string.
Empty;
int _headerrowcount = 1;
_currentrowcount int = 0; 49.50. Public Mergeexcel (String [] sourcefiles, String destfile, string columnend, int headerrowcount) 51. {52.53. Bookdest = (excel.workbookclass) app.
Workbooks.Add (Missing.Value); Sheetdest = BookDest.Worksheets.Add (Missing.Value, Missing.Value, Missing.Value, Missing.Value) as Excel.
worksheet;
Sheetdest.name = "Data"; 56.57.
_sourcefiles = SourceFiles;
_destfile = DestFile; _columnend = CoLumnend;
_headerrowcount = Headerrowcount; 61.62. } 63. <summary> 64. Open Worksheet 65. </summary> 66. <param name= "FileName" ></param> 67. void OpenBook (String fileName) 68. {Booksource = app. Workbooks._open (FileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value 70.) , Missing.Value, Missing.Value, Missing.Value, Missing.Value 71.
, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
Sheetsource = booksource.worksheets[1] as Excel.Worksheet; 73.} 74. <summary> 75. Close Worksheet 76. </summary> 77. void Closebook () 78.
{Booksource.close (False, Missing.Value, Missing.Value); 80.} 81. <summary> 82. Copy table Header 83. </summary> 84. void Copyheader () 85. {86. Excel.Range Range = Sheetsource.get_range ("A1", _columnend + _headerrowcount.tostring ()); Range.
Copy (Sheetdest.get_range ("A1", Missing.Value));
_currentrowcount + = _headerrowcount; 89.} 90. <summary> 91. Copy data 92. </summary> 93. void CopyData () 94.
{A. int sheetrowcount = SheetSource.UsedRange.Rows.Count; Excel.Range Range = Sheetsource.get_range (string.
Format ("a{0}", _headerrowcount + 1), _columnend + sheetrowcount.tostring ()); Range. Copy (Sheetdest.get_range (String).
Format ("a{0}", _currentrowcount + 1), Missing.Value)); _currentrowcount + = range.
Rows.Count; 99.} 100. <summary> 101. Save result 102. </summary> 103. void Save () 104.
{bookdest.saved = true;
Bookdest.savecopyas (_destfile); 107.} 108. <summary> 109. Exit Process 110. </summary> 111. void Quit () 112. {113. App.
Quit (); 114.} 115. <summary> 116. Merged 117. </summary> 118. void Domerge () 119.
{A. bool B = false; 121. foreach (String strfile in _sourcefiles) 122.
{123. OpenBook (strfile); 124. if (b = = false) 125.
{126. Copyheader ();
127. B = true; 128.} 129.
CopyData ();
130. Closebook (); 131.} 132.
Save ();
Quit (); 134.} 135. <summary> 136. Merge Table 137. </summary> 138. <param name= "SourceFiles" > Source file </param> 139. <param name= "DestFile" > Target file </param> 140. <param NAMe= "Columnend" > last column logo </param> 141. <param name= "Headerrowcount" > header row number </param> 142. public static void Domerge (string [] sourcefiles, String destfile, string columnend, int headerrowcount) 143. {144. New Mergeexcel (SourceFiles, DestFile, Columnend, Headerrowcount).
Domerge (); 145.} 146.
} 147. 148.} 149.