public ActionResult Products_ImportFile(HttpPostedFileBase uploadFile) { string filePath = ""; try { int _rel = 1; List <Product_Declaration_Info> lst_product_Dec = new List <Product_Declaration_Info>(); if (uploadFile != null) { if (!string.IsNullOrEmpty(uploadFile.FileName)) { // lấy ds sản phẩm lên trước Product_BL _Product_BL = new Product_BL(); List <Product_Info> _lst_data = _Product_BL.Product_GetAll(); Hashtable _hsProduct = new Hashtable(); foreach (Product_Info item in _lst_data) { _hsProduct[item.Product_Code.ToUpper()] = item; } string _temp = uploadFile.FileName; string _file_name = NaviCommon.CommonFuc.Get_File_name(ref _temp); // Lưu file vao server filePath = Server.MapPath(NaviCommon.Config_Info.c_file_Upload_Sync + _file_name); // Nếu có file rồi delete cho chắc if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } uploadFile.SaveAs(filePath); string file_extension = System.IO.Path.GetExtension(filePath); DataSet ds = new DataSet(); ds = CommonFuc.ReadXlsxFile(filePath); System.IO.File.Delete(filePath); #region Đọc file if (ds.Tables.Count > 0) { int i = 0; foreach (DataRow dr in ds.Tables[0].Rows) { i++; if (i < 3) { // chỉ lấy dòng thứ 3 trở đi continue; } Product_Declaration_Info _PruductInfo = new Product_Declaration_Info(); string _ProductCode = ""; decimal _Pack_Quantity = 0; decimal _Quantity = 0; string _MadeIn = ""; decimal _Value = 0; _ProductCode = dr[0].ToString(); if (!_hsProduct.ContainsKey(_ProductCode.ToUpper())) { // không tồn tại sản phẩm _rel = -10; break; } // số kiện try { _Pack_Quantity = Convert.ToDecimal(dr[1].ToString()); } catch (Exception) { _rel = -11;// ko phải là số break; } // số lượng try { _Quantity = Convert.ToDecimal(dr[2].ToString()); } catch (Exception) { _rel = -11;// ko phải là số break; } //nơi xuất xứ _MadeIn = dr[3].ToString(); try { _Value = Convert.ToDecimal(dr[4].ToString()); } catch (Exception) { _rel = -11;// ko phải là số break; } Product_Info _tempInfo = (Product_Info)_hsProduct[_ProductCode.ToUpper()]; _PruductInfo.Product_Id = _tempInfo.Product_Id; _PruductInfo.Product_Code = _ProductCode; _PruductInfo.Package_Quantity = _Pack_Quantity; _PruductInfo.Quantity = _Quantity; _PruductInfo.Made_In = _MadeIn; _PruductInfo.Value = _Value; lst_product_Dec.Add(_PruductInfo); } #endregion } } } if (_rel > 0) { ViewBag.ListProduct = lst_product_Dec; return(PartialView("/Areas/ModuleDeclaration/Views/YShare/PartialListProduct.cshtml")); } else { return(null); } } catch (Exception ex) { System.IO.File.Delete(filePath); NaviCommon.Common.log.Error(ex.ToString()); return(PartialView("/Areas/ModuleDeclaration/Views/YShare/PartialListProduct.cshtml")); } }