Beispiel #1
0
        /// <summary>
        /// 导入评价
        /// </summary>
        /// <param name="pdtComment"></param>
        /// <returns></returns>
        public bool AddPdtComment(AddPLPLUserPdtComment pdtComment)
        {
            var req = Mapper.Map <AddPLPLUserPdtComment, AddPLPLUserPdtCommentRequest>(pdtComment);
            var res = OpcClient.Send <AddPLPLUserPdtCommentResponse>(req);

            return(res.DoFlag);
        }
        /// <summary>
        /// 批量导入评论
        /// </summary>
        /// <returns></returns>
        public void UploadFile()
        {
            try
            {
                //设置上传目录
                string path = Server.MapPath("~/Content/upload/");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                //判断是否已经选择上传文件
                HttpPostedFileBase file = Request.Files["file"];
                if (file != null && file.ContentLength > 0)
                {
                    string filenName = file.FileName;
                    string fileExt   = Path.GetExtension(filenName).ToLower().Substring(1);
                    if (fileExt != "xls" && fileExt != "xlsx")
                    {
                        ModelState.AddModelError("", "您选择的不是Excel文件");
                        //return View("Index");
                    }
                    else
                    {
                        #region   文件
                        string filepath = path + filenName;
                        file.SaveAs(filepath);
                        //读取excel文件,转换成dataset
                        string strConn;
                        strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath +
                                  ";Extended Properties=Excel 8.0;";
                        OleDbConnection  conn = new OleDbConnection(strConn);
                        OleDbDataAdapter oada = new OleDbDataAdapter("select * from [Sheet1$]", strConn);
                        DataTable        dt   = new DataTable();
                        oada.Fill(dt);
                        #endregion

                        AddPLPLUserPdtComment list = new AddPLPLUserPdtComment();
                        list.AddCommentDos = new List <UserPdtCommentDetail>();
                        #region 循环读取每一行,将数据插入到sql server数据库

                        if (dt != null && dt.Rows.Count > 0)
                        {
                            foreach (DataRow row in dt.Rows)
                            {
                                UserPdtCommentDetail model = new UserPdtCommentDetail();
                                model.IntProductID    = Convert.ToInt32(row[0]);
                                model.VchContent      = row[1].ToString();
                                model.DtCommentDate   = Convert.ToDateTime(row[2]);
                                model.VchEmail        = row[3].ToString();
                                model.IntUserID       = 0;
                                model.IntOrderNO      = 0;
                                model.IntGroupID      = 0;
                                model.VchPdtName      = "";
                                model.IntStart        = 5;
                                model.IntIsMask       = 1;
                                model.IntIsHighLight  = 0;
                                model.IntIndexVisible = 0;
                                model.IntAuditState   = 1;
                                list.AddCommentDos.Add(model);
                            }
                        }
                        #endregion

                        #region 批量导入数据库
                        var res = UserPdtCommentClient.Instance.AddPdtComment(list);
                        if (res)
                        {
                            Response.Write("<script>alert('导入全部成功!');window.location.href='/UserPdtComment/index'</script>");
                        }
                        else
                        {
                            Response.Write("<script>alert('部分导入成功!,修改后再继续导入!');window.location.href='/UserPdtComment/index'</script>");
                        }
                        #endregion
                    }
                }
                else
                {
                    Response.Write("<script>alert('请选择文件!');window.location.href='/UserPdtComment/index'</script>");
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "请选择文件");
                Response.Write("<script>alert('请选择文件!');window.location.href='/UserPdtComment/index'</script>");
            }
        }