• .NET 使用OLEDB导入Excel数据


    /**
    *
    *在本章节中主要讲解的是如何使用OLEDB将Excel中的数据导入到数据库中
    *
    */
    using
    System; using System.Data; using System.Data.OleDb; using System.Data.SqlClient; using System.IO; using System.Text; using System.Web; using System.Web.UI; private DataTable xsldata() { if(fuload.FileName == "") { lbmsg.Text = "请选择文件"; return null; } string fileExtenSion; fileExtenSion = Path.GetExtension(fuload.FileName); if(fileExtenSion.ToLower() != ".xls" && fileExtenSion.ToLower() != ".xlsx") { lbmsg.Text = "上传的文件格式不正确"; return null; } try { string FileName = "App_Data/" + Path.GetFileName(fuload.FileName); if(File.Exists(Server.MapPath(FileName))) { File.Delete(Server.MapPath(FileName)); } fuload.SaveAs(Server.MapPath(FileName)); //HDR=Yes,这代表第一行是标题,不做为数据使用 ,如果用HDR=NO,则表示第一行不是标题,做为数据来使用。系统默认的是YES string connstr2003 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(FileName) + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'"; string connstr2007 = "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 = new OleDbConnection(connstr2003); } else { conn = new OleDbConnection(connstr2007); } conn.Open(); string sql = "select * from [Sheet1$]"; OleDbCommand cmd = new OleDbCommand(sql, conn); DataTable dt = new DataTable(); OleDbDataReader sdr = cmd.ExecuteReader(); dt.Load(sdr); sdr.Close(); conn.Close(); //删除服务器里上传的文件 if(File.Exists(Server.MapPath(FileName))) { File.Delete(Server.MapPath(FileName)); } return dt; } catch(Exception e) { return null; } } protected void Btn_Export_Excel_To_DB_Click(object sender, EventArgs e) { try{ DataTable dt = xsldata(); //dataGridView2.DataSource = ds.Tables[0]; int errorcount = 0;//记录错误信息条数 int insertcount = 0;//记录插入成功条数 int updatecount = 0;//记录更新信息条数 string strcon = "server=localhost;database=database1;uid=sa;pwd=sa"; SqlConnection conn = new SqlConnection(strcon);//链接数据库 conn.Open(); for(int i = 0; i < dt.Rows.Count; i++) { string Name = dt.Rows[i][0].ToString();//dt.Rows[i]["Name"].ToString(); "Name"即为Excel中Name列的表头 string Sex = dt.Rows[i][1].ToString(); int Age = Convert.ToInt32(dt.Rows[i][2].ToString()); string Address = dt.Rows[i][3].ToString(); if(Name != "" && Sex != "" && Age != 0 && Address != "") { SqlCommand selectcmd = new SqlCommand("select count(*) from users where Name='" + Name + "' and Sex='" + Sex + "' and Age='" + Age + "' and Address=" + Address, conn); int count = Convert.ToInt32(selectcmd.ExecuteScalar()); if(count > 0) { updatecount++; } else { SqlCommand insertcmd = new SqlCommand("insert into users(Name,Sex,Age,Address) values('" + Name + "','" + Sex + "'," + Age + ",'" + Address + "')", conn); insertcmd.ExecuteNonQuery(); insertcount++; } } else { errorcount++; } } Response.Write((insertcount + "条数据导入成功!" + updatecount + "条数据重复!" + errorcount + "条数据部分信息为空没有导入!")); } catch(Exception ex) { } }
  • 相关阅读:
    c# 使用MySql的MySqlBulkCopy 出现异常 Loading local data is disabled; this must be enabled on both the client and server sides
    DB2 使用EF Core 查询数据 报错 Object reference not set to an instance of an object.
    关于iis部署的网站访问类型设置
    ABP put与delete类型请求异常 TypeErorr: Failed to fetch
    This request has been blocked; the content must be served over HTTPS.
    Mysql Select的字段必须包含在Group By中如何解决
    企业微信小程序-临时登录凭证校验(code2Session)中获取的userid是加密的
    ABP System.ObjectDisposedException: Cannot access a disposed object.
    SQL server数据库文件(mdfldf)的迁移
    A second operation started on this context before a previous operation completed--ABP
  • 原文地址:https://www.cnblogs.com/Can-daydayup/p/8489579.html
Copyright © 2020-2023  润新知