转:Excel导入SQL数据库完整代码
生活随笔
收集整理的這篇文章主要介紹了
转:Excel导入SQL数据库完整代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
代碼 Excel導入SQL數據庫完整代碼?
??protected?void?studentload_Click(object?sender,?EventArgs?e)
????????{//批量添加學生信息
???????????SqlConnection?conn?=?DB.dataBaseConn();//鏈接數據庫
???????????conn.Open();
?????????????try
?????????????{
???????????????string?fileurl?=?typename(studentFileUpload);//調用typename方法取得excel文件路徑
???????????????DataSet?ds?=?new?DataSet();//取得數據集
???????????????ds?=?xsldata(fileurl);
???????????????int?errorcount?=?0;//記錄錯誤信息條數
???????????????int?insertcount?=?0;//記錄插入成功條數
???????????????int?updatecount?=?0;//記錄更新信息條數
????????????????for?(int?i?=?0;?i?<?ds.Tables[0].Rows.Count;?i++)
???????????????{
???????????????????string?stuid?=?ds.Tables[0].Rows[i][0].ToString();
???????????????????string?stuname?=?ds.Tables[0].Rows[i][1].ToString();
???????????????????string?stusex?=?ds.Tables[0].Rows[i][2].ToString();
???????????????????string?zhuanye?=?ds.Tables[0].Rows[i][3].ToString();
???????????????????string?classname?=?ds.Tables[0].Rows[i][4].ToString();
???????????????????if?(stuid?!=?""?&&?stuname?!=?""?&&?stusex?!=?""?&&?zhuanye?!=?""?&&?classname?!=?"")
???????????????????{
????????????????????????????????????SqlCommand?selectcmd?=?new?SqlCommand("select?count(*)?from?zy_class???where?zhuanye='"?+?zhuanye?+?"'and?classname='"?+?classname?+?"'",?conn);
???????????????????????int?count?=?Convert.ToInt32(selectcmd.ExecuteScalar());
???????????????????????if?(count?>?0)
???????????????????????{
???????????????????????????SqlCommand?selectcmd2?=?new?SqlCommand("select?count(*)?from?stud?where???stuid='"?+?stuid?+?"'",?conn);
??????????????????????????int?count2?=?Convert.ToInt32(selectcmd2.ExecuteScalar());
???????????????????????????if?(count2?>?0)
???????????????????????????{
???????????????????????????????SqlCommand?updatecmd?=?new?SqlCommand("update?stud?set???stuname='"?+?stuname?+?"',stusex='"?+?stusex?+?"',zhuanye='"?+?zhuanyei?+?"',classname='"?+?classname?+?"'???where?stuid='"?+?stuid?+?"'",?conn);
??????????????????????????????updatecmd.ExecuteNonQuery();
???????????????????????????????updatecount++;
???????????????????????????}
??????????????????????????else
??????????????????????????{
???????????????????????????????SqlCommand?insertcmd=?new?SqlCommand("insert?into?stud????values('"?+?stuid?+?"','"?+?stuname?+?"','"?+?stusex?+?"','"?+?zhuanye?+?"','"?+?classname+?"')",?conn);
???????????????????????????????insertcmd.ExecuteNonQuery();
???????????????????????????????insertcount++;
???????????????????????????}
???????????????????????}
???????????????????????else
???????????????????????{
???????????????????????????Response.Write("<script?language='javascript'>alert('專業或班級信息有錯!導入失敗!請檢查!');</script>");
???????????????????????????break;??
??
???????????????????????}
???????????????????}
??????????????????else
???????????????????{
???????????????????????errorcount++;
???????????????????}
??????????????}
???????????????Response.Write("<script?language='javascript'>alert('"?+?insertcount?+?"條數據導入成功!"?+?updatecount?+?"條數據更新成功!"?+?errorcount?+?"條數據部分信息為空沒有導入!');</script>");
???????????}
???????????catch?(Exception?e)
???????????{
???????????????Response.Write("<script?language='javascript'>alert('導入失敗!');</script>");
???????????}
???????????finally
???????????{
?????????????conn.Close();
???????????}????
???????}
??
??//把EXCEL文件上傳到服務器并返回文件路徑
??
??private?String?typename(FileUpload?fileloads)
???????{
???????????string?fullfilename?=?fileloads.PostedFile.FileName;
???????????string?filename?=?fullfilename.Substring(fullfilename.LastIndexOf("\\")?+?1);
??????????string?type?=?fullfilename.Substring(fullfilename.LastIndexOf(".")?+?1);
???????????string?murl?=?"";
???????????if?(type?==?"xls")
??????????{
???????????????fileloads.PostedFile.SaveAs(Server.MapPath("excel")?+?"\\"?+?filename);
??????????????murl?=?(Server.MapPath("excel")?+?"\\"?+?filename).ToString();
???????????}
???????????else
???????????{
???????????????Response.Write("<script?language='javascript'>alert('導入文件格式不對!');</script>");
??
???????????}
???????????return?murl;
???????}
??
??????//把excel數據讀入dataset返回l數據集
???????private?DataSet?xsldata(string?filepath)
???????{
??????????string?strCon?=?"Provider=Microsoft.Jet.OLEDB.4.0;Data?Source="?+?filepath?+?";Extended?Properties='Excel?8.0;IMEX=1'";
??????????System.Data.OleDb.OleDbConnection?Conn?=?new?System.Data.OleDb.OleDbConnection(strCon);
??????????string?strCom?=?"SELECT?*?FROM?[Sheet1$]";
??????????Conn.Open();
??????????System.Data.OleDb.OleDbDataAdapter?myCommand?=?new?System.Data.OleDb.OleDbDataAdapter(strCom,?Conn);
???????????DataSet?ds?=?new?DataSet();
??????????myCommand.Fill(ds,?"[Sheet1$]");
??????????Conn.Close();
??????????return?ds;
?????}?
?
??protected?void?studentload_Click(object?sender,?EventArgs?e)
????????{//批量添加學生信息
???????????SqlConnection?conn?=?DB.dataBaseConn();//鏈接數據庫
???????????conn.Open();
?????????????try
?????????????{
???????????????string?fileurl?=?typename(studentFileUpload);//調用typename方法取得excel文件路徑
???????????????DataSet?ds?=?new?DataSet();//取得數據集
???????????????ds?=?xsldata(fileurl);
???????????????int?errorcount?=?0;//記錄錯誤信息條數
???????????????int?insertcount?=?0;//記錄插入成功條數
???????????????int?updatecount?=?0;//記錄更新信息條數
????????????????for?(int?i?=?0;?i?<?ds.Tables[0].Rows.Count;?i++)
???????????????{
???????????????????string?stuid?=?ds.Tables[0].Rows[i][0].ToString();
???????????????????string?stuname?=?ds.Tables[0].Rows[i][1].ToString();
???????????????????string?stusex?=?ds.Tables[0].Rows[i][2].ToString();
???????????????????string?zhuanye?=?ds.Tables[0].Rows[i][3].ToString();
???????????????????string?classname?=?ds.Tables[0].Rows[i][4].ToString();
???????????????????if?(stuid?!=?""?&&?stuname?!=?""?&&?stusex?!=?""?&&?zhuanye?!=?""?&&?classname?!=?"")
???????????????????{
????????????????????????????????????SqlCommand?selectcmd?=?new?SqlCommand("select?count(*)?from?zy_class???where?zhuanye='"?+?zhuanye?+?"'and?classname='"?+?classname?+?"'",?conn);
???????????????????????int?count?=?Convert.ToInt32(selectcmd.ExecuteScalar());
???????????????????????if?(count?>?0)
???????????????????????{
???????????????????????????SqlCommand?selectcmd2?=?new?SqlCommand("select?count(*)?from?stud?where???stuid='"?+?stuid?+?"'",?conn);
??????????????????????????int?count2?=?Convert.ToInt32(selectcmd2.ExecuteScalar());
???????????????????????????if?(count2?>?0)
???????????????????????????{
???????????????????????????????SqlCommand?updatecmd?=?new?SqlCommand("update?stud?set???stuname='"?+?stuname?+?"',stusex='"?+?stusex?+?"',zhuanye='"?+?zhuanyei?+?"',classname='"?+?classname?+?"'???where?stuid='"?+?stuid?+?"'",?conn);
??????????????????????????????updatecmd.ExecuteNonQuery();
???????????????????????????????updatecount++;
???????????????????????????}
??????????????????????????else
??????????????????????????{
???????????????????????????????SqlCommand?insertcmd=?new?SqlCommand("insert?into?stud????values('"?+?stuid?+?"','"?+?stuname?+?"','"?+?stusex?+?"','"?+?zhuanye?+?"','"?+?classname+?"')",?conn);
???????????????????????????????insertcmd.ExecuteNonQuery();
???????????????????????????????insertcount++;
???????????????????????????}
???????????????????????}
???????????????????????else
???????????????????????{
???????????????????????????Response.Write("<script?language='javascript'>alert('專業或班級信息有錯!導入失敗!請檢查!');</script>");
???????????????????????????break;??
??
???????????????????????}
???????????????????}
??????????????????else
???????????????????{
???????????????????????errorcount++;
???????????????????}
??????????????}
???????????????Response.Write("<script?language='javascript'>alert('"?+?insertcount?+?"條數據導入成功!"?+?updatecount?+?"條數據更新成功!"?+?errorcount?+?"條數據部分信息為空沒有導入!');</script>");
???????????}
???????????catch?(Exception?e)
???????????{
???????????????Response.Write("<script?language='javascript'>alert('導入失敗!');</script>");
???????????}
???????????finally
???????????{
?????????????conn.Close();
???????????}????
???????}
??
??//把EXCEL文件上傳到服務器并返回文件路徑
??
??private?String?typename(FileUpload?fileloads)
???????{
???????????string?fullfilename?=?fileloads.PostedFile.FileName;
???????????string?filename?=?fullfilename.Substring(fullfilename.LastIndexOf("\\")?+?1);
??????????string?type?=?fullfilename.Substring(fullfilename.LastIndexOf(".")?+?1);
???????????string?murl?=?"";
???????????if?(type?==?"xls")
??????????{
???????????????fileloads.PostedFile.SaveAs(Server.MapPath("excel")?+?"\\"?+?filename);
??????????????murl?=?(Server.MapPath("excel")?+?"\\"?+?filename).ToString();
???????????}
???????????else
???????????{
???????????????Response.Write("<script?language='javascript'>alert('導入文件格式不對!');</script>");
??
???????????}
???????????return?murl;
???????}
??
??????//把excel數據讀入dataset返回l數據集
???????private?DataSet?xsldata(string?filepath)
???????{
??????????string?strCon?=?"Provider=Microsoft.Jet.OLEDB.4.0;Data?Source="?+?filepath?+?";Extended?Properties='Excel?8.0;IMEX=1'";
??????????System.Data.OleDb.OleDbConnection?Conn?=?new?System.Data.OleDb.OleDbConnection(strCon);
??????????string?strCom?=?"SELECT?*?FROM?[Sheet1$]";
??????????Conn.Open();
??????????System.Data.OleDb.OleDbDataAdapter?myCommand?=?new?System.Data.OleDb.OleDbDataAdapter(strCom,?Conn);
???????????DataSet?ds?=?new?DataSet();
??????????myCommand.Fill(ds,?"[Sheet1$]");
??????????Conn.Close();
??????????return?ds;
?????}?
?
?
轉載于:https://www.cnblogs.com/jhxk/articles/1805863.html
總結
以上是生活随笔為你收集整理的转:Excel导入SQL数据库完整代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 加班其实不是真正的理由(Coders L
- 下一篇: sql server 2008 报表部署