Exemplo n.º 1
0
        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;
            }
        }