您好,欢迎来到爱go旅游网。
搜索
您的当前位置:首页excel到sqL

excel到sqL

来源:爱go旅游网
public DataSet ExecleDs(string filenameurl, string table) {

SqlConnection con = new SqlConnection(); con.ConnectionString =

\"server='CWJMXT-14\\\\SQLEXPRESS';database='E-learning';uid='sa';pwd='stone123'\";

string strConn = \"Provider=Microsoft.Jet.OleDb.4.0;\" + \"data source=\" + filenameurl + \";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'\"; string strcom = \"select *from [Sheet1$]\";

OleDbConnection conn = new OleDbConnection(strConn); conn.Open();

DataSet ds = new DataSet();

OleDbDataAdapter odda = new OleDbDataAdapter(strcom, conn); odda.Fill(ds, table); return ds;

}

protected void Button4_Click(object sender, EventArgs e) {

SqlConnection con = new SqlConnection(); con.ConnectionString =

\"server='CWJMXT-14\\\\SQLEXPRESS';database='E-learning';uid='sa';pwd='stone123'\"; if (FileUpload1.HasFile == false)//HasFile用来检查FileUpload是否有指定文件 {

Response.Write(\" \"); return;//当无文件时,返回 }

string IsXls =

System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();//System.IO.Path.GetExtension获得文件的扩展名 if (IsXls != \".xls\") {

Response.Write(\"\"); return;//当选择的不是Excel文件时,返回 }

con.Open();

string filename = FileUpload1.FileName; //获取Execle文件名 DateTime日期函数 Label2.Text = filename;

string savePath = Server.MapPath((\"~\\\es\\\\\") + filename);//Server.MapPath 获得虚拟服务器相对路径

FileUpload1.SaveAs(savePath); //SaveAs 将上传的文件内容保存在服务器上 DataSet ds = ExecleDs(savePath, filename); //调用自定义方法 DataRow[] dr = ds.Tables[0].Select(); //定义一个DataRow数组 int rowsnum = ds.Tables[0].Rows.Count; if (rowsnum == 0) {

Response.Write(\"\"); //当Excel表为空时,对用户进行提示 } else {

for (int i = 0; i < dr.Length; i++) {

//string pininputdate = dr[i][\"].ToString();//日期 excel列名【名称不能变,否则就会出错】

string insertstr = \"insert into Tcompany(公司代碼,公司名稱_local,公司名稱_English,統一編號,公司電話,公司地址,創建時間)values('\" + dr[i][0].ToString ()+ \"','\" + dr[i][1].ToString ()+ \"','\" + dr[i][2].ToString ()+ \"','\" + dr[i][3].ToString ()+ \"','\" + dr[i][4].ToString ()+ \"','\" + dr[i][5].ToString ()+ \"','\" + dr[i][6].ToString ()+ \"')\"; //這裡怎麼寫!!!!

SqlCommand cmd = new SqlCommand(insertstr, con); try {

cmd.ExecuteNonQuery(); }

catch (MembershipCreateUserException ex) //捕捉异常 {

Response.Write(\"\"); }

}

Response.Write(\"\"); }

con.Close();

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- igat.cn 版权所有 赣ICP备2024042791号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务