예제 #1
0
        protected void Button1_Click(object sender, EventArgs e)
        {
            DataTable Rebate = DataMgr.GetTableRebate();
            DataRow   drproduct;
            bool      fileIsValid = false;

            //如果确认了文件上传,则判断文件类型是否符合要求
            if (this.FileUpload1.HasFile)
            {
                //获取上传文件的后缀名
                String fileExtension = System.IO.Path.GetExtension(this.FileUpload1.FileName).ToLower();//ToLower是将Unicode字符的值转换成它的小写等效项

                //判断文件类型是否符合要求

                if (fileExtension == ".csv")
                {
                    fileIsValid = true;
                }
                else
                {
                    Response.Write("<script type='text/javascript'>window.parent.alert('文件格式不正确!请上传正确格式的文件')</script>");
                    return;
                }
            }
            //如果文件类型符合要求,则用SaveAs方法实现上传,并显示信息
            if (fileIsValid == true)
            {
                try
                {
                    string name = Server.MapPath("~/uploadxls/") + "ProPrice" + DateTime.Now.ToString().Replace("/", "").Replace(" ", "").Replace(":", "").ToString().Replace("-", "") + ".csv";
                    this.FileUpload1.SaveAs(name);
                    if (File.Exists(name))
                    {
                        DataTable dt = ProductBLL.Search.Searcher.OpenCSV(name);
                        if (dt.Rows.Count > 0)
                        {
                            #region    先检测execl 是否存在数据重复,然后在和表对比

                            var CADPrice = from row in dt.Rows.Cast <DataRow>()

                                           group row by new { CP_CAD = row["CAD"].ToString(), CP_Yeat = row["Date"].ToString(), } into result
                                select new { Peo = result.Key, Count = result.Count() };

                            foreach (var group in CADPrice)
                            {
                                if (Convert.ToInt32(group.Count) > 1)
                                {
                                    sqlCad.Append(group.Peo.CP_CAD + ":" + group.Peo.CP_Yeat + ",");
                                }
                            }



                            if (sqlCad.Length == 0)//没有重复项
                            {
                                string str   = "RE_CAD,RE_Date";
                                string table = "Rebate";

                                DataTable dbRebate = hb.getdate(str, table);//数据库表

                                string    tableinfo  = "baseinfo where type=1";
                                DataTable dbbaseinfo = hb.getdate("ID,basename", tableinfo);//返回编码

                                #region 和数据库表对比
                                for (int i = 0; i < dt.Rows.Count; i++)
                                {
                                    string    ss   = "RE_CAD='" + dt.Rows[i]["CAD"].ToString() + "' and RE_Date='" + dt.Rows[i]["Date"].ToString() + "'";
                                    DataRow[] sRow = dbRebate.Select(ss);


                                    if (sRow.Length != 0)//和Rebate 数据对比
                                    {
                                        sqlCad.Append(dt.Rows[i]["CAD"].ToString() + ":" + dt.Rows[i]["Date"].ToString() + ",");
                                    }
                                    else
                                    {
                                        DataRow[] RowID = dbbaseinfo.Select("basename='" + dt.Rows[i]["CAD"].ToString() + "'");

                                        if (RowID.Count() == 0)
                                        {
                                            sqlCad.Append(dt.Rows[i]["CAD"].ToString() + ",");
                                            n = 1;
                                        }
                                        else
                                        {
                                            drproduct    = Rebate.NewRow();
                                            drproduct[0] = dt.Rows[i]["CAD"].ToString();
                                            drproduct[1] = dt.Rows[i]["back1"].ToString() == "" ? 0 : Convert.ToDecimal(dt.Rows[i]["back1"].ToString());
                                            drproduct[2] = dt.Rows[i]["back2"].ToString() == "" ? 0 : Convert.ToDecimal(dt.Rows[i]["back2"].ToString());
                                            drproduct[3] = dt.Rows[i]["back3"].ToString() == "" ? 0 : Convert.ToDecimal(dt.Rows[i]["back3"].ToString());
                                            drproduct[4] = dt.Rows[i]["back4"].ToString() == "" ? 0 : Convert.ToDecimal(dt.Rows[i]["back4"].ToString());
                                            drproduct[5] = dt.Rows[i]["back5"].ToString() == "" ? 0 : Convert.ToDecimal(dt.Rows[i]["back5"].ToString());
                                            drproduct[6] = dt.Rows[i]["Date"].ToString();
                                            drproduct[7] = RowID[0]["ID"].ToString();
                                            Rebate.Rows.Add(drproduct);
                                        }
                                    }
                                }
                                #endregion

                                #region  执行插入

                                if (sqlCad.Length == 0)//如果没有重复项
                                {
                                    //if (hb.insetpro(sqlpin.ToString()))
                                    if (DataMgr.BulkToDBRebate(Rebate))
                                    {
                                        Response.Write("<script type='text/javascript'>window.parent.alert('上传成功');window.location.href='../products/productRebate.aspx';</script>");
                                        Rebate.Clear();
                                        return;
                                    }
                                    else
                                    {
                                        Response.Write("<script type='text/javascript'>window.parent.alert('上传失败');</script>");
                                        return;
                                    }
                                }
                                else
                                {
                                    if (n == 0)
                                    {
                                        Response.Write("<script type='text/javascript'>window.parent.alert('上传失败!上传数据和数据库存在重复:" + sqlCad.ToString() + "');</script>");
                                    }
                                    else
                                    {
                                        Response.Write("<script type='text/javascript'>window.parent.alert('上传失败!请在编码列表处维护编码,新编码:" + sqlCad.ToString() + "');</script>");
                                    }


                                    sqlCad.Clear();
                                    Rebate.Clear();
                                    return;
                                }

                                #endregion
                            }
                            else
                            {
                                Response.Write("<script type='text/javascript'>window.parent.alert('上传失败!返利表存在重复:" + sqlCad.ToString() + "');</script>");

                                sqlCad.Clear();
                                Rebate.Clear();
                                return;
                            }



                            #endregion
                        }
                        else
                        {
                            Response.Write("<script type='text/javascript'>window.parent.alert('文件无内容');</script>");
                            return;
                        }
                    }
                }
                catch
                {
                    //Response.Write("<script type='text/javascript'>window.parent.alert('文件内容格式不正确:" + Label1.Text.ToString() + "');</script>");
                    Response.Write("<script type='text/javascript'>window.parent.alert('文件内容格式不正确')</script>");
                    return;
                }
                finally
                {
                    Rebate.Dispose();
                }
            }
        }