private void ExcelFile(HttpContext context) { string _delfile = AXRequest.GetString("DelFilePath"); HttpPostedFile _upfile = context.Request.Files["Filedata"]; bool _iswater = false; //默认不打水印 bool _isthumbnail = false; //默认不生成缩略图 int id = 0; if (_upfile == null) { context.Response.Write("{\"status\": 0, \"msg\": \"请选择要上传文件!\"}"); return; } // Common.Log.LogMsg("1", "admin"); StringBuilder sb = new StringBuilder(220000); StringBuilder info = new StringBuilder(220000); // Common.Log.LogMsg("start", "super"); UpLoad upFiles = new UpLoad(); string newpath = ""; string msg = upFiles.fileSaveAs(_upfile, _isthumbnail, _iswater, ref newpath); // Common.Log.LogMsg("newpath:" + newpath, "super"); DataTable dt = Common.ExcelHelper.ConvertExcelFileToTable(newpath); BLL.article bll = new BLL.article(); BLL.article_category bbc = new BLL.article_category(); // Common.Log.LogMsg("count:"+dt.Rows.Count.ToString(), "super"); // Common.Log.LogMsg("row:" + dt.Rows[0][0].ToString().Trim(), "super"); for (int i = 1; i < dt.Rows.Count; i++) { int type = 0; string no = ""; if (dt.Rows[i]["Name"].ToString().Trim() == "" && dt.Rows[i]["Barcode"].ToString().Trim() == "") { continue; } if (dt.Rows[i]["Name"].ToString().Trim() == "") { sb.Append("{"); sb.Append("\"id\":\"" + i + "\","); sb.Append("\"code\":\"" + dt.Rows[i]["Barcode"].ToString().Trim() + "\","); sb.Append("\"name\":\"" + dt.Rows[i]["Name"].ToString().Trim() + "\","); sb.Append("\"result\":\"失败\","); sb.Append("\"reason\":\"中文名不能为空\""); sb.Append("},"); continue; } if (dt.Rows[i]["Barcode"].ToString().Trim() == "") { sb.Append("{"); sb.Append("\"id\":\"" + i + "\","); sb.Append("\"code\":\"" + dt.Rows[i]["Barcode"].ToString().Trim() + "\","); sb.Append("\"name\":\"" + dt.Rows[i]["Name"].ToString().Trim() + "\","); sb.Append("\"result\":\"失败\","); sb.Append("\"reason\":\"条形码不能为空\""); sb.Append("},"); continue; } else { if (bll.ExistsBarcode(dt.Rows[i]["Barcode"].ToString().Trim())) { // sb.Append("{"); // sb.Append("\"id\":\"" + i + "\","); //sb.Append("\"code\":\"" + dt.Rows[i]["Barcode"].ToString().Trim() + "\","); //sb.Append("\"name\":\"" + dt.Rows[i]["Name"].ToString().Trim() + "\","); //sb.Append("\"result\":\"失败\","); // sb.Append("\"reason\":\"此商品已存在\""); // sb.Append("},"); // continue; type = 1; } } if (dt.Rows[i]["ProductTypeNo"].ToString().Trim() == "") { sb.Append("{"); sb.Append("\"id\":\"" + i + "\","); sb.Append("\"code\":\"" + dt.Rows[i]["Barcode"].ToString().Trim() + "\","); sb.Append("\"name\":\"" + dt.Rows[i]["Name"].ToString().Trim() + "\","); sb.Append("\"result\":\"失败\","); sb.Append("\"reason\":\"商品类型不能为空\""); sb.Append("},"); continue; } else { try { no = dt.Rows[i]["ProductTypeNo"].ToString().Trim(); if (string.IsNullOrEmpty(no)) { id = 0; } else { id = bbc.GetID(no); } if (id == 0) { sb.Append("{"); sb.Append("\"id\":\"" + i + "\","); sb.Append("\"code\":\"" + dt.Rows[i]["Barcode"].ToString().Trim() + "\","); sb.Append("\"name\":\"" + dt.Rows[i]["Name"].ToString().Trim() + "\","); sb.Append("\"result\":\"失败\","); sb.Append("\"reason\":\"商品类型不存在\""); sb.Append("},"); continue; } } catch (Exception ex) { sb.Append("{"); sb.Append("\"id\":\"" + i + "\","); sb.Append("\"code\":\"" + dt.Rows[i]["Barcode"].ToString().Trim() + "\","); sb.Append("\"name\":\"" + dt.Rows[i]["Name"].ToString().Trim() + "\","); sb.Append("\"result\":\"失败\","); sb.Append("\"reason\":\"商品类型:" + ex.Message + "\""); sb.Append("},"); continue; } } // Common.Log.LogMsg("1", "super"); Model.article model = new Model.article(); Dictionary <string, string> dn = new Dictionary <string, string>(); if (type == 1) { model = bll.GetModelNew(" where Barcode='" + dt.Rows[i]["Barcode"].ToString().Trim() + "'"); dn = model.fields; if (dt.Rows[i]["Quantity"].ToString().Trim() == "") { // dn.Add("stock_quantity", "0"); } else { Decimal d = Convert.ToDecimal(model.fields["stock_quantity"]) + Convert.ToDecimal(dt.Rows[i]["Quantity"].ToString().Trim()); dn["stock_quantity"] = d.ToString(); } if (dt.Rows[i]["PriceMode"].ToString().Trim() == "") { dn["PriceMode"] = "0"; } else { dn["PriceMode"] = dt.Rows[i]["PriceMode"].ToString().Trim(); } if (dt.Rows[i]["PurchasePrice"].ToString().Trim() == "") { dn["PurchasePrice"] = "0"; } else { dn["PurchasePrice"] = dt.Rows[i]["PurchasePrice"].ToString().Trim(); } if (dt.Rows[i]["WholesalePrice"].ToString().Trim() == "") { dn["WholesalePrice"] = "0"; } else { dn["WholesalePrice"] = dt.Rows[i]["WholesalePrice"].ToString().Trim(); } if (dt.Rows[i]["SpecialPrice"].ToString().Trim() == "") { dn["SpecialPrice"] = "0"; } else { dn["SpecialPrice"] = dt.Rows[i]["SpecialPrice"].ToString().Trim(); } if (dt.Rows[i]["DiscountRate"].ToString().Trim() == "") { dn["DiscountRate"] = "0"; } else { dn["DiscountRate"] = dt.Rows[i]["DiscountRate"].ToString().Trim(); } dn["Barcode"] = dt.Rows[i]["Barcode"].ToString().Trim(); dn["goods_no"] = dt.Rows[i]["No"].ToString().Trim(); dn["english_name"] = dt.Rows[i]["NameP"].ToString().Trim(); dn["ProductTypeNo"] = dt.Rows[i]["ProductTypeNo"].ToString().Trim(); dn["sub_title"] = dt.Rows[i]["Name"].ToString().Trim(); } else { model.img_url = "/upload/templates/default.jpg"; model.sort_id = 99; model.click = 0; model.status = 0; model.is_msg = 0; model.is_top = 0; model.is_red = 0; model.is_hot = 0; model.is_slide = 0; if (dt.Rows[i]["Quantity"].ToString().Trim() == "") { dn.Add("stock_quantity", "0"); } else { dn.Add("stock_quantity", dt.Rows[i]["Quantity"].ToString().Trim()); } if (dt.Rows[i]["PriceMode"].ToString().Trim() == "") { dn.Add("PriceMode", "0"); } else { dn.Add("PriceMode", dt.Rows[i]["PriceMode"].ToString().Trim()); } if (dt.Rows[i]["PurchasePrice"].ToString().Trim() == "") { dn.Add("PurchasePrice", "0"); } else { dn.Add("PurchasePrice", dt.Rows[i]["PurchasePrice"].ToString().Trim()); } if (dt.Rows[i]["WholesalePrice"].ToString().Trim() == "") { dn.Add("WholesalePrice", "0"); } else { dn.Add("WholesalePrice", dt.Rows[i]["WholesalePrice"].ToString().Trim()); } if (dt.Rows[i]["SpecialPrice"].ToString().Trim() == "") { dn.Add("SpecialPrice", "0"); } else { dn.Add("SpecialPrice", dt.Rows[i]["SpecialPrice"].ToString().Trim()); } //if (dt.Rows[i]["RetailPrice"].ToString().Trim() == "") //{ // dn.Add("RetailPrice", "0"); //} //else //{ // dn.Add("RetailPrice", dt.Rows[i]["RetailPrice"].ToString().Trim()); //} //if (dt.Rows[i]["TaxRate"].ToString().Trim() == "") //{ // dn.Add("TaxRate", "0"); //} //else //{ // dn.Add("TaxRate", dt.Rows[i]["TaxRate"].ToString().Trim()); //} if (dt.Rows[i]["DiscountRate"].ToString().Trim() == "") { dn.Add("DiscountRate", "0"); } else { dn.Add("DiscountRate", dt.Rows[i]["DiscountRate"].ToString().Trim()); } dn.Add("Barcode", dt.Rows[i]["Barcode"].ToString().Trim()); dn.Add("goods_no", dt.Rows[i]["No"].ToString().Trim()); dn.Add("english_name", dt.Rows[i]["NameP"].ToString().Trim()); dn.Add("ProductTypeNo", dt.Rows[i]["ProductTypeNo"].ToString().Trim()); dn.Add("sub_title", dt.Rows[i]["Name"].ToString().Trim()); model.fields = dn; } model.channel_id = 2; model.category_id = id; model.title = dt.Rows[i]["Name"].ToString(); model.zhaiyao = dt.Rows[i]["Remark"].ToString().Trim(); model.add_time = DateTime.Now; model.is_sys = 1; //管理员发布 model.user_name = "super"; //获得当前登录" + Resources.lang.username+ " // Common.Log.LogMsg("2", "super"); model.fields["sell_price"] = GetPrice(model.fields); List <Model.user_group_price> priceList = new List <Model.user_group_price>(); decimal _price = Convert.ToDecimal(model.fields["sell_price"]); priceList.Add(new Model.user_group_price { group_id = 1, price = _price }); priceList.Add(new Model.user_group_price { group_id = 2, price = _price }); model.group_price = priceList; if (type == 1) { if (!bll.Update(model)) { sb.Append("{"); sb.Append("\"id\":\"" + i + "\","); sb.Append("\"code\":\"" + dt.Rows[i]["Barcode"].ToString().Trim() + "\","); sb.Append("\"name\":\"" + dt.Rows[i]["Name"].ToString().Trim() + "\","); sb.Append("\"result\":\"更新失败\","); sb.Append("\"reason\":\"请检查数据格式\""); sb.Append("},"); continue; } type = 0; } else { if (bll.Add(model) == 0) { sb.Append("{"); sb.Append("\"id\":\"" + i + "\","); sb.Append("\"code\":\"" + dt.Rows[i]["Barcode"].ToString().Trim() + "\","); sb.Append("\"name\":\"" + dt.Rows[i]["Name"].ToString().Trim() + "\","); sb.Append("\"result\":\"新增失败\","); sb.Append("\"reason\":\"请检查数据格式\""); sb.Append("},"); continue; } } } string data = sb.ToString(); if (data.Length > 0) { data = data.Substring(0, data.Length - 1); msg = "{\"status\": 1, \"msg\": \"导入成功!\",\"data\":[" + data + "]}"; } else { msg = "{\"status\": 1, \"msg\": \"部分数据导入失败!\",\"data\":[" + data + "]}"; } // Common.Log.LogMsg(msg, "admin"); ////删除已存在的旧文件 //if (!string.IsNullOrEmpty(_delfile)) //{ // Utils.DeleteUpFile(_delfile); //} //返回成功信息 context.Response.Write(msg); context.Response.End(); }