usingSystem;usingSystem.Data;usingSystem.Data.OleDb;usingSystem.Data.SqlClient;usingSystem.IO;usingSystem.Text;usingsystem.web;usingSystem.Web.UI;PrivateDataTable Xsldata () {if(Fuload. FileName = ="") {lbmsg. Text="Please select a file"; return NULL; } stringfileextension; FileExtension=path.getextension (fuload. FileName); if(Fileextension.tolower ()! =". xls"&& fileextension.tolower ()! =". xlsx") {lbmsg. Text="the uploaded file is not in the correct format"; return NULL; } Try { stringFileName ="app_data/"+Path.getfilename (fuload. FileName); if(file.exists (Server.MapPath (filename))) {File.delete (Server.MapPath (filename)); } fuload. SaveAs (Server.MapPath (FileName)); //Hdr=yes, this means that the first row is the title, not as data use, if used Hdr=no, then the first row is not the title, as data to use. The system default is Yes stringconnstr2003 ="Provider=Microsoft.Jet.OLEDB.4.0;Data source="+ Server.MapPath (FileName) +"; Extended properties= ' Excel 8.0; hdr=yes;imex=1; '"; stringconnstr2007 ="Provider=microsoft.ace.oledb.12.0;data source="+ Server.MapPath (FileName) +"; Extended properties=\ "Excel 12.0; Hdr=yes\ ""; OleDbConnection Conn; if(Fileextension.tolower () = =". xls") {Conn=NewOleDbConnection (connstr2003); } Else{conn=NewOleDbConnection (connstr2007); } conn. Open (); stringsql ="select * FROM [sheet1$]"; OleDbCommand cmd=NewOleDbCommand (SQL, conn); DataTable DT=NewDataTable (); OleDbDataReader SDR=cmd. ExecuteReader (); Dt. Load (SDR); Sdr. Close (); Conn. Close (); //Delete files uploaded from the server if(file.exists (Server.MapPath (filename))) {File.delete (Server.MapPath (filename)); } returnDT; } Catch(Exception e) {return NULL; } } protected voidBtn_export_excel_to_db_click (Objectsender, EventArgs e) { Try{DataTable dt=Xsldata (); //Datagridview2.datasource = ds. Tables[0]; intErrorcount =0;//record the number of error messages intInsertcount =0;//record number of insert success bars intUpdatecount =0;//record the number of update information bars stringStrcon ="Server=localhost;database=database1;uid=sa;pwd=sa"; SqlConnection Conn=NewSqlConnection (Strcon);//Link DatabaseConn. Open (); for(inti =0; i < dt. Rows.Count; i++) { stringName = dt. rows[i][0]. ToString ();//dt. rows[i]["Name"]. ToString (); "Name" is the header of the Name column in Excel stringSex = dt. rows[i][1]. ToString (); intAge = Convert.ToInt32 (dt. rows[i][2]. ToString ()); stringAddress = dt. rows[i][3]. ToString (); if(Name! =""&& Sex! =""&& Age! =0&& Address! ="") {SqlCommand selectcmd=NewSqlCommand ("Select COUNT (*) from users where Name= '"+ Name +"' and sex= '"+ Sex +"' and age= '"+ Age +"' and address="+Address, conn); intCount =Convert.ToInt32 (selectcmd. ExecuteScalar ()); if(Count >0) {Updatecount++; } Else{SqlCommand Insertcmd=NewSqlCommand ("INSERT into users (name,sex,age,address) VALUES ('"+ Name +"', '"+ Sex +"',"+ Age +", '"+ Address +"')", conn); Insertcmd. ExecuteNonQuery (); Insertcount++; } } Else{Errorcount++; }} Response.Write ((Insertcount+"Data import successfully! "+ Updatecount +"duplicate the Data bar! "+ Errorcount +"The data section information is empty and not imported! ")); } Catch(Exception ex) {}}
. NET import of Excel data