A complete example of using WPF to read and write excel-excel file merging Tool

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.