Example #1
0
        private void btnExcel_Click(object sender, EventArgs e)
        {
            if (txtPwd.Text == "")
            {
                MessageBox.Show("请填写用户默认密码", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Filter = "Excel文件|*.xls";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        sha1pwd     = GetSHA1(txtPwd.Text.Trim());
                        label1.Text = "正在导入Excel数据....";
                        filePath    = openFileDialog.FileName;
                        DataTable resultDT = users.SelectUsersByUID("");
                        DataTable sourceDT = users.SelectAllUsers("");
                        resultDT.Clear();
                        ExcelHelper eh = new ExcelHelper();
                        memberDT = eh.ExcelToDataTable_Member(filePath, resultDT, sha1pwd);
                        MemberDT = resultDT.Clone();

                        foreach (DataRow dr in memberDT.Rows)
                        {
                            Business.BaseData.Store stores = new Business.BaseData.Store();
                            stores.StoreDT = stores.SelectAllStoreInfo();

                            //上属经理
                            Boolean boolFlag = true;
                            foreach (DataRow drUsers in memberDT.Rows)
                            {
                                if (dr["ManagerID"].ToString() == drUsers["UID"].ToString() && dr["UID"].ToString() != "Administrator")
                                {
                                    //所属店铺
                                    if (dr["Character"].ToString() == "4")
                                    {
                                        foreach (DataRow drStore in stores.StoreDT.Rows)
                                        {
                                            if (dr["Store"].ToString() == drStore["StoreName"].ToString())
                                            {
                                                MemberDT.Rows.Add(dr.ItemArray);
                                                boolFlag = false;
                                                break;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        MemberDT.Rows.Add(dr.ItemArray);
                                        boolFlag = false;
                                        break;
                                    }
                                }
                            }
                            if (boolFlag)
                            {
                                Boolean boolFlag2 = true;
                                foreach (DataRow drUsers in sourceDT.Rows)
                                {
                                    if (dr["ManagerID"].ToString() == drUsers["UID"].ToString() && dr["UID"].ToString() != "Administrator")
                                    {
                                        //所属店铺
                                        if (dr["Character"].ToString() == "4")
                                        {
                                            foreach (DataRow drStore in stores.StoreDT.Rows)
                                            {
                                                if (dr["Store"].ToString() == drStore["StoreName"].ToString())
                                                {
                                                    MemberDT.Rows.Add(dr.ItemArray);
                                                    boolFlag2 = false;
                                                    break;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            MemberDT.Rows.Add(dr.ItemArray);
                                            boolFlag2 = false;
                                            break;
                                        }
                                    }
                                }
                                //所属店铺
                                if (dr["Character"].ToString() == "4" && boolFlag2)
                                {
                                    foreach (DataRow drStore in stores.StoreDT.Rows)
                                    {
                                        if (dr["Store"].ToString() == drStore["StoreName"].ToString())
                                        {
                                            MemberDT.Rows.Add(dr.ItemArray);
                                            break;
                                        }
                                    }
                                }
                            }
                        }

                        dgvMember.AutoGenerateColumns = false;
                        dgvMember.DataSource          = MemberDT;
                        label1.Text = "Excel数据导入完成";
                    }
                    catch (Exception ex)
                    {
                        label1.Text = "Excel数据导入失败";
                    }
                }
            }
        }