asp.net 实现excel导入到数据库

//Excel批量导入到数据库
      protected void btnUpload_Click(object sender, EventArgs e)
      {   
         string IsXls = System.IO.Path.GetExtension(UploadExcel.FileName.ToString().ToLower());
         if (IsXls != ".xls")
         {
             Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script type='text/javascript' language='javascript'>alert('只可以选择Excel文件');location.href='EmployeeAddPage.aspx';</script>");
               return;//当选择的不是Excel文件时,返回
         }
          string filePath = UploadExcel.PostedFile.FileName;
          string fileName = UploadExcel.FileName;
          DataSet ds = ExecleDs (filePath,fileName);

          CommonLayer.employeeModel model = new CommonLayer.employeeModel();
          DataAcessLayer.employee_infomation addlist = new DataAcessLayer.employee_infomation();

          if (ds.Tables[0].Rows.Count > 0)
          {

              int row = ds.Tables[0].Rows.Count-1;
              for (int i = 0; i < ds.Tables[0].Select().Length; i++)
              {
                  string employee_no = ds.Tables[0].Rows[i]["employee_no"].ToString();
                  string employee_login_name = ds.Tables[0].Rows[i]["employee_login_name"].ToString();
                  string employee_display_name = ds.Tables[0].Rows[i]["employee_display_name"].ToString();
                  string employee_account = ds.Tables[0].Rows[i]["employee_account"].ToString();
                  string employee_mail = ds.Tables[0].Rows[i]["employee_mail"].ToString();
                  string active_flag = ds.Tables[0].Rows[i]["active_flag"].ToString();
                  string password = ds.Tables[0].Rows[i]["password"].ToString();
                  string employee_role = ds.Tables[0].Rows[i]["employee_role"].ToString();

                  #region 导入数据
                  // 判断是否存在工号,登陆名,NT,email重复
                  if (addlist.ExistsInfo(employee_no, employee_login_name, employee_account, employee_mail))
                  {
                      AlertAndRedirect("信息填写重复,请重新输入!!!", "EmployeeAddPage.aspx");

                      
                     
                  }

                  // 判断表 是否填写完整
                  else if (employee_account==""||
                      employee_no == "" ||
                      employee_mail == "" ||
                      employee_login_name == "" ||
                      employee_display_name == "")
                  {
                      AlertAndRedirect("请将信息填写完整!!!","EmployeeAddPage.aspx");

                  }
                  else
                  {
                      // 将Excel中的数据写到数据库中

                   }
              AlertAndRedirect("导入成功!!", "FrmEmployeeList.aspx");
          }

          else
          {
              AlertAndRedirect("Excel表为空!", "EmployeeAddPage.aspx");
          }
      }
       
      public void InsertExcelStaue(string filenameurl, string employeeNo,string statue)
      {
          string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=2'";
          OleDbConnection conn = new OleDbConnection(strConn);
          conn.Open();
          OleDbCommand cmd = new OleDbCommand(" update [Sheet1$] set statue='" + statue + "' where employee_no='"+employeeNo+"'", conn);
          
          cmd.ExecuteNonQuery();
          conn.Close();
          
      }

      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=2'";
          OleDbConnection conn = new OleDbConnection(strConn);
          conn.Open();
          DataSet ds = new DataSet();
          OleDbDataAdapter odda = new OleDbDataAdapter("select * from [Sheet1$]", conn);
          odda.Fill(ds, table);
          return ds;

    }

   public void AlertAndRedirect(string str, string redirectUrl)
      {
          HttpContext.Current.Response.Write("<script language='javascript'>alert('" + str + "');location.href='"+redirectUrl+"';</script>");
      }
      public void Alert(string str)
      {
          HttpContext.Current.Response.Write("<script language='javascript'>alert('" + str + "');</script>");
      }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值