Exemple #1
0
        public BaseDataPackage <mproduct> MproductImport()
        {
            var result = new BaseDataPackage <mproduct>();

            HttpContextBase    context  = (HttpContextBase)Request.Properties["MS_HttpContext"]; //获取传统context
            HttpRequestBase    request  = context.Request;                                       //定义传统request对象
            string             UserID   = request.Form["UserID"];
            HttpFileCollection filelist = HttpContext.Current.Request.Files;

            if (filelist != null && filelist.Count > 0)
            {
                HttpPostedFile file     = filelist[0];
                string         Tpath    = "/Import/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
                string         filename = file.FileName;
                string         FileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                string         FilePath = HttpContext.Current.Server.MapPath("~/" + Tpath);
                string         type     = System.IO.Path.GetExtension(filename);
                DirectoryInfo  di       = new DirectoryInfo(FilePath);
                if (!di.Exists)
                {
                    di.Create();
                }

                List <mproduct> list = null;
                //Dictionary<string, string> ob = new Dictionary<string, string>();
                try
                {
                    string tmpFileName = FilePath + FileName + type;
                    file.SaveAs(tmpFileName);

                    MProduct sche = new MProduct();
                    list = this.ExcelToMproductList(tmpFileName, UserID);
                    if (list.Count > 0)
                    {
                        int ret = sche.ImportMProductList(list, out int addNum, out int updNum, out int cfNum);

                        //result.Keys = ob;
                        result.Status = ApiStatusCode.OK;
                        if (cfNum > 0)
                        {
                            result.Message = string.Format("数据导入成功,新增数据: {0}条, 修改数据: {1}条, Excel中有 {2} 条重复数据未导入", addNum, updNum, cfNum);
                        }
                        else
                        {
                            result.Message = string.Format("数据导入成功,新增数据: {0}条, 修改数据: {1}条", addNum, updNum);
                        }
                    }
                    else
                    {
                        result.Status  = ApiStatusCode.FAIL;
                        result.Message = "导入数据失败,请确认所选文件是否正确";
                    }
                    return(result);
                }
                catch (Exception ex)
                {
                    result.DataList = null;
                    result.Status   = ApiStatusCode.FAIL;
                    result.Message  = "导入数据失败:" + ex.Message;
                    return(result);
                }
            }
            else
            {
                result.Status  = ApiStatusCode.EXCEPTION;
                result.Message = "上传导入文件出现异常!";
                return(result);
            }
        }