"Go" C # Excel import into an Access database table (version WinForm)

Source: Internet
Author: User

   /// <summary>         ///Get Excel file/// </summary>         /// <param name= "Sender" ></param>         /// <param name= "E" ></param>         Private voidButton1_Click (Objectsender, EventArgs e) {OpenFileDialog Dlg=NewOpenFileDialog (); Dlg. Filter="Excel File (*.xls) |*.xls"; if(DLG. ShowDialog () = =DialogResult.OK) {stringFilePath =dlg.                 FileName;  This. TextBox1.Text =FilePath; }         }         /// <summary>         ///Import an Excel file/// </summary>         /// <param name= "Sender" ></param>         /// <param name= "E" ></param>         Private voidButton2_Click (Objectsender, EventArgs e) {             if(TextBox1.Text.Length = =0) {MessageBox.Show ("Please select the execl file for importing data"); }             Else             {                 Try{oledbconnectionstringbuilder Connectstringbuilder=NewOledbconnectionstringbuilder (); Connectstringbuilder.datasource= This. TextBox1.Text.Trim (); Connectstringbuilder.provider="microsoft.jet.oledb.4.0"; Connectstringbuilder.add ("Extended Properties","Excel 8.0"); using(OleDbConnection cn =NewOleDbConnection (connectstringbuilder.connectionstring)) {DataSet DS=NewDataSet (); stringsql ="Select * FROM [sheet1$]"; OleDbCommand cmdliming=NewOleDbCommand (SQL, CN); cn.                         Open (); using(OleDbDataReader drliming =Cmdliming.executereader ()) {ds. Load (drliming, Loadoption.overwritechanges,New string[] {"Sheet1" }); DataTable DT= ds. tables["Sheet1"]; if(dt. Rows.Count >0)                             {                                  for(inti =0; i < dt. Rows.Count; i++)                                 {                                     //Write database Data                                    stringMYSQL ="INSERT into clientinfo values ('"+dt. rows[i]["name"]. ToString () +"', '"+dt. rows[i]["name"]. ToString ()+"' , ' 0 ', '"+dt. rows[i]["Notes"]. ToString () +"' , ' 0 ', '"+i.tostring () +"')"; Newdataaccess ().                                 SQLExecute (MYSQL); } MessageBox.Show ("Data Import Successful! "); }                             Else{MessageBox.Show ("Check to see if there is data in your Excel"); }                         }                     }                 }                 Catch(Exception ex) {MessageBox.Show (ex).                 ToString ()); }             }         } /// <summary>         ///data manipulation General class/// </summary>         /// <param name= "SQL" ></param>         /// <returns></returns>          Public BOOLSQLExecute (stringSQL) {             Try{OleDbConnection conn=NewOleDbConnection (connect_string); Conn.                 Open (); OleDbCommand Comm=NewOleDbCommand (); Comm. Connection=Conn; Comm.commandtext=SQL; Comm.                 ExecuteNonQuery (); Comm.                 Connection.close (); Conn.                 Close (); return true; }             Catch             {                 return false; }         }
Related Article

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.