public ActionResult CustomerImport(HttpPostedFileBase file, int overType = 0, int type=0) { if (file == null) { return Content("请选择要导入的文件"); } if (file.ContentLength > 2097152) { return Content("导入文件超过规定(2M )大小,请修改后再操作."); } if (!file.FileName.Contains(".xls") && !file.FileName.Contains(".xlsx")) { return Content("文件格式类型不正确"); } string mes = ""; try { DataTable dt = ImportExcelToDataTable(file); if (dt.Columns.Count > 0) { ///1.获取系统模版列 var checkColumn = dt.Columns.Contains("是否企业客户"); Dictionary<string, ExcelFormatter> dic = new Dictionary<string, ExcelFormatter>(); Dictionary<string, ExcelModel> listColumn = GetColumnForJson("customer", ref dic, checkColumn ? "Item" : "OwnItem", "import", CurrentUser.ClientID); ///2.上传Excel 与模板中列是否一致 不一致返回提示 foreach (DataColumn dc in dt.Columns) { if (default(KeyValuePair<string, ExcelModel>).Equals(listColumn.FirstOrDefault(x => x.Value.Title == dc.ColumnName))) { mes += dc.ColumnName + ","; } } if (!string.IsNullOrEmpty(mes)) { return Content("Excel模版与系统模版不一致,请重新下载模板,编辑后再上传.错误:缺少列 " + mes); } ///3.开始处理 int k = 1; List<CustomerEntity> list = new List<CustomerEntity>(); List<ContactEntity> contactList = new List<ContactEntity>(); var excelWriter = new ExcelWriter(); foreach (DataRow dr in dt.Rows) { try { if (checkColumn) { CustomerEntity customer = new CustomerEntity(); excelWriter.GetProductByDataRow(dr, listColumn, customer, dic, CurrentUser.ClientID); customer.ClientID = CurrentUser.ClientID; customer.AgentID = CurrentUser.AgentID; customer.CreateUserID = CurrentUser.UserID; customer.CreateTime = DateTime.Now; customer.CustomerID = Guid.NewGuid().ToString(); customer.CityCode = GetCityCode(dr); customer.OwnerID = customer.OwnerID.Trim() == "是" ? CurrentUser.UserID : ""; list.Add(customer); //上面出问题的话请注释以上 使用下面 //list.Add(GetCustomerByDataRow(dr)); } else { ContactEntity contact = new ContactEntity(); excelWriter.GetProductByDataRow(dr, listColumn, contact, dic, CurrentUser.ClientID); contact.CityCode = GetCityCode(dr); contact.ClientID = CurrentUser.ClientID; contact.AgentID = CurrentUser.AgentID; contact.CreateUserID = CurrentUser.UserID; contact.OwnerID = CurrentUser.UserID; contact.CustomerID = ""; contact.CreateTime = DateTime.Now; contact.Type = 0; contact.ContactID = Guid.NewGuid().ToString(); contactList.Add(contact); //contactList.Add( GetContactByDataRow(dr, checkColumn)); } } catch (Exception ex) { mes += k +" 原因:"+ex.Message+ ","; } } try { if (list.Count > 0) ExcelImportBusiness.InsertCustomer(list, type, overType); if (contactList.Count > 0) ExcelImportBusiness.InsertContact(contactList, type, overType); } catch (Exception ex) { return Content("系统异常:请联系管理员,错误原因" + ex.Message); } } if (!string.IsNullOrEmpty(mes)) { return Content("部分数据未导入成功,Excel行位置" + mes); } } catch (Exception ex) { return Content("系统异常:请联系管理员,错误原因:" + ex.Message.ToString()); } return Content("操作成功"); }
public ActionResult ProductImport(HttpPostedFileBase file) { string mes = ""; if (file == null) { return Content("请选择要导入的文件"); } if (file.ContentLength > 2097152) { return Content("导入文件超过规定(2M )大小,请修改后再操作."); } if (!file.FileName.Contains(".xls") && !file.FileName.Contains(".xlsx")) { return Content("文件格式类型不正确"); } try { DataTable dt = ImportExcelToDataTable(file); if (dt.Columns.Count > 0) { ///1.获取系统模版列 Dictionary<string, ExcelFormatter> dic=new Dictionary<string, ExcelFormatter>(); Dictionary<string, ExcelModel> listColumn = GetColumnForJson("product", ref dic, "", "import", CurrentUser.ClientID); ;//GetColumnForJson("product", "Item"); ///2.上传Excel 与模板中列是否一致 不一致返回提示 foreach (DataColumn dc in dt.Columns) { if (default(KeyValuePair<string, ExcelModel>).Equals(listColumn.FirstOrDefault(x => x.Value.Title == dc.ColumnName))) { mes += dc.ColumnName + ","; } } if (!string.IsNullOrEmpty(mes)) { return Content("Excel模版与系统模版不一致,请重新下载模板,编辑后再上传.错误:缺少列 " + mes); } ///3.开始处理 int k = 1; var excelWriter = new ExcelWriter(); List<Products> list = new List<Products>(); foreach (DataRow dr in dt.Rows) { try { Products product=new Products(); excelWriter.GetProductByDataRow(dr, listColumn, product,dic,CurrentUser.ClientID); product.CreateUserID = CurrentUser.UserID; product.ClientID = CurrentUser.ClientID; product.BigUnitID = ""; list.Add(product); } catch (Exception ex) { mes += k +" 原因:"+ex.Message+ ","; } } try { if (list.Count > 0) { mes= ExcelImportBusiness.InsertProduct(list); } if (!string.IsNullOrEmpty(mes)) { return Content("部分数据未导入成功,原因如下 :" + mes); } else { return Content("操作成功"); } } catch (Exception ex) { return Content("系统异常:请联系管理员,错误原因" + ex.Message); } } if (!string.IsNullOrEmpty(mes)) { return Content("部分数据未导入成功,Excel行位置" + mes); } } catch (Exception ex) { return Content("系统异常:请联系管理员,错误原因:" + ex.Message.ToString()); } return Content(mes); }