.net 导入excel数据

C#

浏览数:46

2019-9-2

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) 
            { 
  
            } 
        }

 

作者:追逐时光