转:Excel导入SQL数据库完整代码
Excel导入SQL数据库完整代码 2 protected void studentload_Click(object sender, EventArgs e) 3 {//批量添加学生信息 4 SqlConnection conn = DB.dataBaseConn();//链接数据库 5 conn.Open(); 6 try 7 { 8 string fileurl = typename(studentFileUpload);//调用typename方法取得excel文件路径 9 DataSet ds = new DataSet();//取得数据集 10 ds = xsldata(fileurl); 11 int errorcount = 0;//记录错误信息条数 12 int insertcount = 0;//记录插入成功条数 13 int updatecount = 0;//记录更新信息条数 14 for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 15 { 16 string stuid = ds.Tables[0].Rows[i][0].ToString(); 17 string stuname = ds.Tables[0].Rows[i][1].ToString(); 18 string stusex = ds.Tables[0].Rows[i][2].ToString(); 19 string zhuanye = ds.Tables[0].Rows[i][3].ToString(); 20 string classname = ds.Tables[0].Rows[i][4].ToString(); 21 if (stuid != "" && stuname != "" && stusex != "" && zhuanye != "" && classname != "") 22 { 23 SqlCommand selectcmd = new SqlCommand("select count(*) from zy_class where zhuanye='" + zhuanye + "'and classname='" + classname + "'", conn); 24 int count = Convert.ToInt32(selectcmd.ExecuteScalar()); 25 if (count > 0) 26 { 27 SqlCommand selectcmd2 = new SqlCommand("select count(*) from stud where stuid='" + stuid + "'", conn); 28 int count2 = Convert.ToInt32(selectcmd2.ExecuteScalar()); 29 if (count2 > 0) 30 { 31 SqlCommand updatecmd = new SqlCommand("update stud set stuname='" + stuname + "',stusex='" + stusex + "',zhuanye='" + zhuanyei + "',classname='" + classname + "' where stuid='" + stuid + "'", conn); 32 updatecmd.ExecuteNonQuery(); 33 updatecount++; 34 } 35 else 36 { 37 SqlCommand insertcmd= new SqlCommand("insert into stud values('" + stuid + "','" + stuname + "','" + stusex + "','" + zhuanye + "','" + classname+ "')", conn); 38 insertcmd.ExecuteNonQuery(); 39 insertcount++; 40 } 41 } 42 else 43 { 44 Response.Write("<script language='javascript'>alert('专业或班级信息有错!导入失败!请检查!');</script>"); 45 break; 46 47 } 48 } 49 else 50 { 51 errorcount++; 52 } 53 } 54 Response.Write("<script language='javascript'>alert('" + insertcount + "条数据导入成功!" + updatecount + "条数据更新成功!" + errorcount + "条数据部分信息为空没有导入!');</script>"); 55 } 56 catch (Exception e) 57 { 58 Response.Write("<script language='javascript'>alert('导入失败!');</script>"); 59 } 60 finally 61 { 62 conn.Close(); 63 } 64 } 65 66 //把EXCEL文件上传到服务器并返回文件路径 67 68 private String typename(FileUpload fileloads) 69 { 70 string fullfilename = fileloads.PostedFile.FileName; 71 string filename = fullfilename.Substring(fullfilename.LastIndexOf("\\") + 1); 72 string type = fullfilename.Substring(fullfilename.LastIndexOf(".") + 1); 73 string murl = ""; 74 if (type == "xls") 75 { 76 fileloads.PostedFile.SaveAs(Server.MapPath("excel") + "\\" + filename); 77 murl = (Server.MapPath("excel") + "\\" + filename).ToString(); 78 } 79 else 80 { 81 Response.Write("<script language='javascript'>alert('导入文件格式不对!');</script>"); 82 83 } 84 return murl; 85 } 86 87 // 88 89 把excel数据读入dataset返回l数据集 90 91 private DataSet xsldata(string filepath) 92 { 93 string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;IMEX=1'"; 94 System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection(strCon); 95 string strCom = "SELECT * FROM [Sheet1$]"; 96 Conn.Open(); 97 System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter(strCom, Conn); 98 DataSet ds = new DataSet(); 99 myCommand.Fill(ds, "[Sheet1$]"); 100 Conn.Close(); 101 return ds; 102 } 103 104 105 106 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/HelloXiaoyu/archive/2009/01/15/3790134.aspx