示例#1
0
    protected void Button3_Click(object sender, EventArgs e)
    {
        int    successfulcount = 0, errorcount = 0;
        string location = "";

        try
        {
            if (this.FileUpload1.HasFile)
            {
                if (!ue.SaveUpFile("~/Admin/temp/", this.FileUpload1))//上传文件
                {
                    throw new Exception("上传文件失败!");
                }

                DataTable inputdt = ue.InputExcel("员工信息");
                if (inputdt == null)
                {
                    throw new Exception("Excel文件读取失败!");
                }

                int           j;
                CustomerBll   cust = new CustomerBll();
                tab_customers customer;
                StringBuilder erroritem = new StringBuilder();

                for (int i = 0; i < inputdt.Rows.Count; i++)
                {
                    j        = 0;
                    customer = new tab_customers();
                    customer.customerCode = inputdt.Rows[i][j++].ToString().Trim();//0
                    customer.customerName = inputdt.Rows[i][j++].ToString().Trim();
                    location = customer.customerName + "customerEmail";
                    customer.customerEmail = inputdt.Rows[i][j++].ToString().Trim();
                    location = customer.customerName + "customerPrivateEmail";
                    customer.customerPrivateEmail = inputdt.Rows[i][j++].ToString().Trim();
                    location = customer.customerName + "customerMobile";
                    customer.customerMobile = inputdt.Rows[i][j++].ToString().Trim();
                    location                        = customer.customerName + "customerTel";
                    customer.customerTel            = inputdt.Rows[i][j++].ToString().Trim();//5
                    customer.customerProvince       = inputdt.Rows[i][j++].ToString().Trim();
                    customer.customerCity           = inputdt.Rows[i][j++].ToString().Trim();
                    customer.customerZone           = inputdt.Rows[i][j++].ToString().Trim();
                    customer.customerAddress        = inputdt.Rows[i][j++].ToString().Trim();
                    customer.customerAllAddr        = inputdt.Rows[i][j++].ToString().Trim();//10
                    customer.customerDefaultAddr    = inputdt.Rows[i][j++].ToString().Trim();
                    customer.customerGender         = inputdt.Rows[i][j++].ToString().Trim();
                    customer.customerMarriageStatus = inputdt.Rows[i][j++].ToString().Trim();
                    location                        = customer.customerName + "customerDOB";
                    if (inputdt.Rows[i][j] == null || inputdt.Rows[i][j].ToString() == "")
                    {
                        //customer.customerDOB = DateTime.Parse("1800-1-1");
                        j++;
                    }
                    else
                    {
                        customer.customerDOB = DateTime.Parse(inputdt.Rows[i][j++].ToString().Trim());
                    }
                    customer.customerNation = inputdt.Rows[i][j++].ToString().Trim();//15
                    location = customer.customerName + "customerIDcard";
                    customer.customerIDcard = inputdt.Rows[i][j++].ToString().Trim();
                    //if (customer.customerIDcard =="")
                    //{
                    //    this.Label1.Text = "身份证不能为空";
                    //    continue;
                    //}
                    string    sq = "select customerIDcard from tab_customers where customerIDcard='" + customer.customerIDcard + "'";
                    DataTable ds = cb.CustomerSelect(sq);
                    if (ds.Rows.Count > 0 && customer.customerIDcard != "")
                    {
                        erroritem.Append("身份证重复" + customer.customerIDcard + ",\n");
                        this.Label1.Text = erroritem.ToString();
                        continue;
                    }
                    if (customer.customerIDcard.Length == 18)
                    {
                        try
                        {
                            string sid = customer.customerIDcard;
                            customer.customerDOB    = DateTime.Parse(sid.Substring(6, 4) + "-" + sid.Substring(10, 2) + "-" + sid.Substring(12, 2));
                            customer.customerGender = (int.Parse(sid.Substring(16, 1)) % 2 == 1) ? "男" : "女";
                        }
                        catch
                        { }
                    }


                    customer.customerCompany     = inputdt.Rows[i][j++].ToString().Trim();
                    customer.customerCompanycode = inputdt.Rows[i][j++].ToString().Trim();
                    customer.customerClass       = inputdt.Rows[i][j++].ToString().Trim();
                    location = customer.customerName + "customerCompanyTel";
                    customer.customerCompanyTel      = inputdt.Rows[i][j++].ToString().Trim();//20
                    customer.customerCompanyProvince = inputdt.Rows[i][j++].ToString().Trim();
                    customer.customerCompanyCity     = inputdt.Rows[i][j++].ToString().Trim();
                    customer.customerCompanyZone     = inputdt.Rows[i][j++].ToString().Trim();
                    customer.customerCompanyAddress  = inputdt.Rows[i][j++].ToString().Trim();
                    customer.customerCompanyBU       = inputdt.Rows[i][j++].ToString().Trim();//25
                    customer.customerVIP             = inputdt.Rows[i][j++].ToString().Trim();
                    location = customer.customerName + "customerBalance";
                    customer.customerBalance = double.Parse(inputdt.Rows[i][j++].ToString().Trim());
                    location                = customer.customerName + "packagecode";
                    customer.packagecode    = inputdt.Rows[i][j++].ToString().Trim();
                    location                = customer.customerName + "customerBudget";
                    customer.customerBudget = double.Parse(inputdt.Rows[i][j++].ToString().Trim());
                    try
                    {
                        if (cust.CustomerAdd(customer) < 1)
                        {
                            errorcount++;
                        }
                        else
                        {
                            successfulcount++;
                        }
                    }
                    catch
                    {
                        errorcount++;
                        erroritem.Append(customer.customerCode + ",\n");
                    }
                }


                GridView1.DataBind();
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", " <script>alert('操作已完成!其中成功导入数据" + successfulcount + "条,失败" + errorcount + "条" + erroritem.ToString() + "'); </script>");
            }
            else
            {
                throw new Exception("请选择Excel文件!");
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", " <script>alert('error:" + ex.Message + location + "'); </script>");
        }
    }