Exemple #1
0
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            string name    = tb_name.Text.Trim();
            string phone   = tb_phone.Text.Trim();
            string email   = tb_email.Text.Trim();
            string address = tb_address.Text.Trim();
            string zipcode = tb_zipcode.Text.Trim();
            bool   result  = false;

            if (basicInfoId != 0)//修改
            {
                result = BasicInfoBusiness.UpdateBasicInfo(basicInfoId, name, phone, email, address, zipcode);
                if (!result)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alter", "alert('修改失败,请重新再试!')", true);
                    return;
                }
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alter", "parent.layer.msg('修改成功', 2, -1);close();", true);
            }
            else//新增
            {
                result = BasicInfoBusiness.AddBasicInfo(name, phone, email, address, zipcode);
                if (!result)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alter", "alert('新增失败,请重新再试!')", true);
                    return;
                }
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alter", "parent.layer.msg('新增成功', 2, -1);close();", true);
            }
        }
        protected void btn_importEvent_Click(object sender, EventArgs e)
        {
            string fileExtension = null;
            string file          = import_upload.FileName.Replace(" ", "");
            string filename      = System.IO.Path.GetFileNameWithoutExtension(file) + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace(" ", "").Replace("/", "").Replace(":", "").Replace("-", "");
            bool   fileOK        = false;
            string savepath      = Server.MapPath("~/Upload/DepartmentPlace/");

            //判断是否已选择上传文件
            if (import_upload.HasFile)
            {
                //获得文件后缀
                fileExtension = System.IO.Path.GetExtension(import_upload.FileName).ToLower();
                //允许上传文件后缀
                String[] allowedExtensions = { ".xls", ".xlsx" };
                //判断文件后缀是否被允许上传
                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (fileExtension == allowedExtensions[i])
                    {
                        fileOK    = true;
                        filename += fileExtension;
                    }
                }
                //文件类型合法
                if (fileOK)
                {
                    try
                    {
                        //long strLen = btn_upload.PostedFile.ContentLength;//字节
                        //if (strLen >= sizes)
                        //{
                        //    ScriptManager.RegisterStartupScript(UpdatePanel_basic, this.GetType(), "alter", "alert('上传照片不能大于5M!')", true);
                        //    return;
                        //}
                        if (!Directory.Exists(savepath))
                        {
                            Directory.CreateDirectory(savepath);
                        }
                        import_upload.PostedFile.SaveAs(savepath + filename);

                        DataTable     dt           = ReadExcelToTable(savepath + filename); //读取Excel文件(.xls和.xlsx格式)
                        int           success      = 0;
                        int           fail         = 0;
                        StringBuilder sb_failIndex = new StringBuilder();
                        string        errorInfo    = "。";
                        for (int i = 1; i < dt.Rows.Count; i++)
                        {
                            string name    = dt.Rows[i]["F1"].ToString();
                            string phone   = dt.Rows[i]["F2"].ToString();
                            string email   = dt.Rows[i]["F3"].ToString();
                            string address = dt.Rows[i]["F4"].ToString();
                            string zipcode = dt.Rows[i]["F5"].ToString();
                            if (name.IsNullOrEmpty() || (phone.IsNullOrEmpty() && email.IsNullOrEmpty()))
                            {
                                fail++;
                                sb_failIndex.Append((i + 1) + "." + name + "、");
                                continue;
                            }

                            if (BasicInfo.FindOne(CK.K["Name"] == name && CK.K["IsDelete"] == false) == null)
                            {
                                if (BasicInfoBusiness.AddBasicInfo(name, phone, email, address, zipcode))
                                {
                                    success++;
                                    continue;
                                }
                                else
                                {
                                    fail++;
                                    sb_failIndex.Append((i + 1) + "." + name + "、");
                                    continue;
                                }
                            }
                            else
                            {
                                fail++;
                                sb_failIndex.Append((i + 1) + "." + name + "、");
                                continue;
                            }
                        }
                        if (sb_failIndex.Length > 0)
                        {
                            sb_failIndex.Remove(sb_failIndex.Length - 1, 1);
                            errorInfo = ",失败excel序号(包含标题行)为:" + sb_failIndex.ToString();
                        }
                        RefreshView(0);
                        ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "提示", "layer.alert('" + "导入完成!总共" + (success + fail) + "条,其中成功导入" + success + "条,失败导入" + fail + "条" + errorInfo + "')", true);
                        return;
                    }
                    catch (Exception ex)
                    {
                        text(ex.ToString());
                        ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "提示", "layer.alert('无法上传!')", true);
                        return;
                    }
                }
                else
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "提示", "layer.alert('未识别的文件类型!请使用.xls或.xlsx文件进行导入!')", true);
                    return;
                }
            }
            else
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "提示", "layer.alert('请选择文件!')", true);
                return;
            }
        }