.net Excle导入数据库

 string strConn = "Data Source=.;DataBase=hhaspx;Integrated Security = true"; //链接SQL数据库
    public DataSet ExecleDs(string filenameurl, string table)
    {
        string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
        string sqlstr = "select * from [Sheet1$]";
        OleDbConnection conn = new OleDbConnection(strConn);
        conn.Open();
        DataSet dataset = new DataSet();
        OleDbDataAdapter da = new OleDbDataAdapter(sqlstr,conn);
        da.Fill(dataset,table);
        return dataset;
    }
    protected void btnDR_Click(object sender, EventArgs e)
    {
        //判断是否存在Excel文件
        if (FileUpload1.HasFile == false)//HasFile 检查FileUpload是否有指定文件
        {
            Response.Write("<script language = javascript>alert('请您选择Excel文件')</script>");
            return;
        }
        //检查文件是否问.xls 后缀的Excel文件
        string IsXls = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();//System.IO.Path.GetExtension获得文件的扩展名
        if (IsXls != ".xls")
        {
            Response.Write("<script language=javascript>alert('您只可以选择Excel文件!')</script>");
            return;
        }
        SqlConnection sqlconn = new SqlConnection(strConn);
        sqlconn.Open();
        //获取Excel DateTime日期函数
        string filename = DateTime.Now.ToString("yyyymmddhhMMss") + FileUpload1.FileName;
        //Server.MapPath 获得虚拟服务器相对路径
        string savePath = Server.MapPath(("~//upfiles//")+filename);
        //SaveAs 将上传的文件内容保存在服务器上
        FileUpload1.SaveAs(savePath);
        //调用自定义方法
        DataSet ds = ExecleDs(savePath,filename);
        //定义一个DataRow数组
        DataRow[] dr = ds.Tables[0].Select();
        int rowsnum = ds.Tables[0].Rows.Count;
        //判断Excel内容是否为空
        if (rowsnum == 0)
        {
            Response.Write("<script>alert('Excel表为空表,无数据!')</script>");   //当Excel表为空时,对用户进行提示
        }
        else
        {
            for (int i = 0; i < dr.Length; i++)
            {
                string hhaspx_rq = dr[i]["日期"].ToString();
                string hhaspx_bh = dr[i]["编号"].ToString();
                string hhaspx_xm = dr[i]["姓名"].ToString();
                string hhaspx_dx = dr[i]["底薪"].ToString();
                string hhaspx_kh = dr[i]["考核"].ToString();
                string hhaspx_jl = dr[i]["奖励"].ToString();
                string hhaspx_jt = dr[i]["津贴"].ToString();
                string hhaspx_jb = dr[i]["加班"].ToString();
                string hhaspx_zb = dr[i]["值班"].ToString();
                string hhaspx_jx = dr[i]["绩效"].ToString();
                string hhaspx_hj = dr[i]["合计"].ToString();
                string sqlcheck = "select count(*) from hhaspx_gz where hhaspx_rq ='"+hhaspx_rq+"' and hhaspx_xm = '"+hhaspx_xm+"'";//检查用户是否存在
                SqlCommand sqlcmd = new SqlCommand(sqlcheck,sqlconn);
                int count = Convert.ToInt32(sqlcmd.ExecuteScalar());
                if (count < 1)
                {
                    string insertstr = "insert into hhaspx_gz (hhaspx_rq,hhaspx_bh,hhaspx_xm,hhaspx_dx,hhaspx_kh,hhaspx_jl,hhaspx_jt,hhaspx_jb,hhaspx_zb,hhaspx_jx,hhaspx_hj) values('" + hhaspx_rq + "','" + hhaspx_bh + "','" + hhaspx_xm + "','" + hhaspx_dx + "','" + hhaspx_kh + "','" + hhaspx_jl + "','" + hhaspx_jt + "','" + hhaspx_jb + "','" + hhaspx_zb + "','" + hhaspx_jx + "','" + hhaspx_hj + "')";
                    SqlCommand cmd = new SqlCommand(insertstr, sqlconn);
                    try
                    {
                        cmd.ExecuteNonQuery();
                    }
                    catch (MembershipCreateUserException ex) //捕捉成员未成功创建时的异常
                    {
                        Response.Write("<script language = javascript>alert('导入内容:" + ex.Message + "')</script>");
                    }
                }
                else
                {
                    Response.Write("<script language = javascript>alert('内容重复!禁止导入!');location='Default.aspx'</script>");
                    continue;
                }
            }
            Response.Write("<script>alert('Excle表导入成功!');location='default.aspx'</script>");
        }
        sqlconn.Close();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值