Using System. data; using System. data. oleDb; using System. IO; using System. text; using System. windows; namespace excel file merging tool {// <summary> // MainWindow. interaction logic of xaml // </summary> public partial class MainWindow: Window {public MainWindow () {this. initializeComponent (); // Insert the Code required to create an object under this vertex .} // Select the private void buttings click (object sender, System. Windows. RoutedEventArgs e) folder {// Add the event handler implementation here. System. windows. forms. folderBrowserDialog fd = new System. windows. forms. folderBrowserDialog (); if (fd. showDialog () = System. windows. forms. dialogResult. OK) lbl1.Text = fd. selectedPath;} // read the private void but2_Click (object sender, System. windows. routedEventArgs e) {// Add the event handler implementation here. If (lbl1.Text = string. empty | lbl2.Text = string. empty) {MessageBox. show ("file path and worksheet name cannot be blank", "NOTE", MessageBoxButton. OK, MessageBoxImage. information); return;} string [] strs = Directory. getFiles (lbl1.Text ,"*. xls "); string err = string. empty; DataTable rt = new DataTable (); foreach (string s in strs) {string strconn = "Provider = Microsoft. jet. OLEDB.4.0; Data Source = "+ s +"; Extended Properties = \ "E Xcel 8.0; HDR = No; IMEX = 1; \ ""; OleDbConnection conn = new OleDbConnection (strconn ); string SQL = "SELECT * FROM [" + lbl2.Text + "$]"; OleDbDataAdapter adp = new OleDbDataAdapter (SQL, conn); DataTable dt = new DataTable (); try {adp. fill (dt);} catch {err + = s + "," ;}finally {conn. close ();} rt. load (new DataTableReader (dt);} grid1.ItemsSource = rt. defaultView; grid1.CanUserAddRows = false; if (e Rr! = String. empty) MessageBox. show ("the following files encountered problems during merging. Please check whether the file format is correct \ n" + err, "NOTE", MessageBoxButton. OK, MessageBoxImage. information);} // export the new xls file private void but2_Copy_Click (object sender, System. windows. routedEventArgs e) {// Add the event handler implementation here. System. windows. forms. saveFileDialog sf = new System. windows. forms. saveFileDialog (); sf. filter = "excel document | *. xls "; if (sf. showDialog () = System. windows. forms. dialogResult. OK) {FileStream fs = new FileStream (sf. fileName, FileMode. create, FileAccess. write); StreamWriter sw = new StreamWriter (fs, Encoding. default); DataTable dt = (grid1.ItemsSource as DataView ). table; foreach (DataRow dr in dt. rows) {String line = string. empty; foreach (DataColumn dc in dt. columns) {line + = dr [dc] + "\ t";} sw. writeLine (line);} sw. close (); fs. close (); MessageBox. show ("data has been exported successfully! "," NOTE ", MessageBoxButton. OK, MessageBoxImage. Information );}}}}
Recently, I often use the vlookup () function of excel to find data. Some data is scattered in different tables, which is difficult to copy and paste, the tool is used to merge files in the same folder. Click to download