public ActionResult BatchImportExcel(HttpPostedFileBase file) { try { Tools.Config.UploadConfig config = UploadProvice.Instance(); SiteOption option = config.SiteOptions["local"]; string filePath = option.Folder + config.Settings["file"].FilePath + DateTime.Now.ToString("yyyyMMddHHmmss") + (file.FileName.IndexOf(".xlsx") > 0 ? ".xlsx" : ".xls"); string result = FileHelper.ValicationAndSaveFileToPath(file, filePath); if (result != "0") { return(MessageBoxAndJump($"上传失败,{result}", "list")); } var sheet = ExcelHelper.ReadExcel(filePath); var modelList = BatchExcelToList(sheet, 1); if (modelList == null || modelList.Count < 1) { return(MessageBoxAndJump("数据为空,导入失败!", "list")); } if (Bll.BllSupplier_List.Insert(modelList) < 1) { return(MessageBoxAndJump("导入失败", "list")); } } catch (Exception ex) { return(MessageBoxAndJump("导入失败:" + ex.Message.ToString(), "list")); } return(Redirect("list")); }
public ActionResult List(HttpPostedFileBase file) { try { Tools.Config.UploadConfig config = UploadProvice.Instance(); SiteOption option = config.SiteOptions["local"]; string filePath = option.Folder + config.Settings["file"].FilePath + DateTime.Now.ToString("yyyyMMddHHmmss") + (file.FileName.IndexOf(".xlsx") > 0 ? ".xlsx" : ".xls"); string result = FileHelper.ValicationAndSaveFileToPath(file, filePath); if (result != "0") { return(MessageBoxAndJump($"上传失败,{result}", "list")); } var sheet = ExcelHelper.ReadExcel(filePath); string msg = ExcelToList(sheet, 5); if (msg != "0") { return(MessageBoxAndJump("导入失败," + GetMsg(msg), "list")); } } catch (Exception ex) { return(MessageBoxAndJump("导入失败:" + ex.Message.ToString(), "list")); } return(Redirect("list")); }
/// <summary> /// 验证并保存上传的文件到指定文件夹 /// </summary> /// <param name="file"></param> /// <param name="path"></param> /// <returns></returns> public static string ValicationAndSaveFileToPath(HttpPostedFileBase file, string filePath) { if (file == null) { return("上传文件为空"); } try { var size = file.ContentLength; int maxFileSize = UploadProvice.Instance().Settings["file"].MaxFileSize; //判断文件大小和格式 if (size > maxFileSize) { return("上传的文件太大"); } if (!CheckFilesRealFormat.ValidationFile(file)) { return("上传的文件格式不正确"); } file.SaveAs(filePath); return("0"); } catch (Exception ex) { return("上传错误!"); } }
public ActionResult SingleImportExcel(HttpPostedFileBase file) { try { Tools.Config.UploadConfig config = UploadProvice.Instance(); SiteOption option = config.SiteOptions["local"]; string filePath = option.Folder + config.Settings["file"].FilePath + DateTime.Now.ToString("yyyyMMddHHmmss") + (file.FileName.IndexOf(".xlsx") > 0 ? ".xlsx" : ".xls"); string result = FileHelper.ValicationAndSaveFileToPath(file, filePath); if (result != "0") { return(MessageBoxAndJump($"上传失败,{result}", "list")); } var sheet = Tools.Tool.ExcelHelper.ReadExcel(filePath); var modelList = SingleExcelToList(sheet, 1, ref result); if (modelList == null) { return(MessageBoxAndJump($"导入失败,{result}!", "list")); } result = Bll.BllSupplier_VendorInfo.AddVendorInfo(modelList); if (result != "0") { return(MessageBoxAndJump("导入失败," + result, "list")); } } catch (Exception) { return(MessageBoxAndJump("导入失败:请联系管理员", "list")); } return(Redirect("list")); }
public ActionResult ImportProduct(HttpPostedFileBase file) { try { int contractId = RequestInt("contractId"); if (contractId == 0) { return(LayerAlertErrorAndReturn("请选择一个合同!")); } var contract = Bll.BllOrder_Contract.First(o => o.Id == contractId); if (contract == null) { return(LayerAlertErrorAndClose("合同不存在!")); } Tools.Config.UploadConfig config = UploadProvice.Instance(); SiteOption option = config.SiteOptions["local"]; string filePath = option.Folder + config.Settings["file"].FilePath + DateTime.Now.ToString("yyyyMMddHHmmss") + (file.FileName.IndexOf(".xlsx") > 0 ? ".xlsx" : ".xls"); string result = FileHelper.ValicationAndSaveFileToPath(file, filePath); if (result != "0") { return(MessageBoxAndReturn($"上传失败,{result}!")); } var sheet = Tools.Tool.ExcelHelper.ReadExcel(filePath); if (sheet == null) { return(MessageBoxAndReturn($"Excel读取失败,请检查格式!")); } int updateCount = 0; List <Order_ContractProduct> list = new List <Order_ContractProduct>(); IRow row; for (int i = 1; i <= sheet.LastRowNum; i++) //从第二行开始读取 { row = sheet.GetRow(i); //第i行数据 if (row != null) { var name = row.GetCell(0).ToString(); if (!string.IsNullOrEmpty(name)) { var price = row.GetCell(1).ToString().ToDecimal(); var quantity = row.GetCell(2).ToString().ToInt32(); var model = Bll.BllOrder_ContractProduct.First(o => o.Name == name && o.ContractId == contractId); if (model != null) { model.Quantity += quantity; model.Price += price; model.TotalPrice = model.Quantity * model.Price; if (Bll.BllOrder_ContractProduct.Update(model) > 0) { updateCount++; } } else { list.Add(new Order_ContractProduct() { ContractId = contractId, Name = name, Price = price, Quantity = quantity, TotalPrice = price * quantity, AddTime = DateTime.Now, AdminId = MyInfo.Id }); } } } } if (Bll.BllOrder_ContractProduct.Insert(list) > 0 || updateCount > 0) { return(MessageBoxAndReturn("导入成功!")); } else { return(MessageBoxAndReturn("导入失败,请联系管理员!")); } } catch (Exception ex) { return(MessageBoxAndReturn("导入失败,请联系管理员!")); } }
public override void Process() { byte[] uploadFileBytes = null; string uploadFileName = null; if (UploadConfig.Base64) { uploadFileName = UploadConfig.Base64Filename; uploadFileBytes = Convert.FromBase64String(Request[UploadConfig.UploadFieldName]); } else { var file = Request.Files[0];//Request.Files[UploadConfig.UploadFieldName]; uploadFileName = file.FileName; if (!CheckFileType(uploadFileName)) { Result.State = UploadState.TypeNotAllow; WriteResult(); return; } if (!CheckFileSize(file.ContentLength)) { Result.State = UploadState.SizeLimitExceed; WriteResult(); return; } uploadFileBytes = new byte[file.ContentLength]; try { file.InputStream.Read(uploadFileBytes, 0, file.ContentLength); } catch (Exception) { Result.State = UploadState.NetworkError; WriteResult(); } } Result.OriginFileName = uploadFileName; string fileExt = Path.GetExtension(uploadFileName).ToLower(); string saveDoc = DateTime.Now.ToString(@"yy\\MM\\dd\\"); string rndFileName = FancyFix.Tools.Usual.Common.GetDataShortRandom(); string dirPath = UploadConfig.PathFormat;// uploadFileName;//PathFormatter.Format(uploadFileName, UploadConfig.PathFormat); string newFileName = saveDoc + rndFileName + fileExt; string newSmallFileName = saveDoc + rndFileName + "s" + fileExt; var savePath = PathFormatter.Format(uploadFileName, UploadConfig.PathFormat); try { if (!Directory.Exists(dirPath + saveDoc)) { Directory.CreateDirectory(dirPath + saveDoc); } File.WriteAllBytes(dirPath + newFileName, uploadFileBytes); ImageTools.SetImgSize(dirPath + newFileName, 800, 800); if (UploadConfig.PathFormat.IndexOf("product") > 0) { Setting setting = UploadProvice.Instance().Settings["product"]; if (setting.AddWaterMark) { ImageTools.AddWaterMark(dirPath + newFileName, dirPath + newFileName, Server.MapPath(setting.WaterMarkImgOrTxt), WaterMarkType.ImageMark, WaterMarkPosition.Right_Bottom, setting.Transparency); } } //Tools.Tool.ImageTools.CreateSmallImage(dirPath + newFileName, dirPath + newSmallFileName, 200, 200); Result.Url = FancyFix.Tools.Usual.Utils.GetLastFolderName(dirPath) + "/" + newFileName; Result.State = UploadState.Success; } catch (Exception e) { Result.State = UploadState.FileAccessError; Result.ErrorMessage = e.Message; FancyFix.Tools.Tool.LogHelper.WriteLog(typeof(UploadHandler), e); } finally { WriteResult(); } }
public ActionResult ImportProduct(HttpPostedFileBase file) { try { int classId = RequestInt("classId"); if (classId == 0) { return(LayerAlertErrorAndReturn("请选择一个分类!")); } var classModel = Bll.BllProduct_Class.First(o => o.Id == classId); if (classModel == null) { return(LayerAlertErrorAndClose("分类不存在!")); } if (classModel.ChildNum > 0) { return(LayerAlertErrorAndReturn("请选择一个最底层分类!")); } Tools.Config.UploadConfig config = UploadProvice.Instance(); SiteOption option = config.SiteOptions["local"]; string filePath = option.Folder + config.Settings["file"].FilePath + DateTime.Now.ToString("yyyyMMddHHmmss") + (file.FileName.IndexOf(".xlsx") > 0 ? ".xlsx" : ".xls"); string result = FileHelper.ValicationAndSaveFileToPath(file, filePath); if (result != "0") { return(MessageBoxAndReturn($"上传失败,{result}!")); } var sheet = Tools.Tool.ExcelHelper.ReadExcel(filePath); if (sheet == null) { return(MessageBoxAndReturn($"Excel读取失败,请检查格式!")); } if (sheet.LastRowNum < 1) { return(MessageBoxAndReturn($"Excel无数据!")); } int succuessCount = 0; List <Product_Info> list = new List <Product_Info>(); IRow row; for (int i = 1; i <= sheet.LastRowNum; i++) //从第二行开始读取 { row = sheet.GetRow(i); //第i行数据 if (row != null) { string title = row.GetCell(0)?.ToString(); if (string.IsNullOrEmpty(title)) { continue; //标题为空,跳过 } if (Bll.BllProduct_Info.Any(o => o.Title == title)) { continue; //标题和老SPU已存在,跳过 } //产品Spu var classIds = classModel.ParPath.TrimEnd(',').Split(','); string spu = Bll.BllProduct_Class.GetSpuCode(classId); if (string.IsNullOrEmpty(spu)) { continue; //spu编码生成失败,跳过 } if (Bll.BllProduct_Info.IsExistsSpu(spu)) { continue; //Spu重复,跳过 } string titleEn = row.GetCell(1)?.ToString(); string oldSpu = row.GetCell(2)?.ToString(); string supplierCode = row.GetCell(3)?.ToString(); string supplierName = row.GetCell(4)?.ToString(); string specification = row.GetCell(5)?.ToString(); string priceRemark = row.GetCell(6)?.ToString(); string hsCode = row.GetCell(7)?.ToString(); string InvoiceName = row.GetCell(8)?.ToString(); string firstPic = row.GetCell(9)?.ToString(); var model = new Product_Info() { ClassId_1 = classIds.Length > 0 ? classIds[0].ToInt32() : 0, ClassId_2 = classIds.Length > 1 ? classIds[1].ToInt32() : 0, ClassId = classModel.Id, ClassParPath = classModel.ParPath, Title = title, Title_En = titleEn, IsShow = true, Old_Spu = oldSpu, Spu = spu, SupplierProductCode = supplierCode, SupplierName = supplierName, Specification = specification, PriceRemark = priceRemark, HS_Code = hsCode, InvoiceName = InvoiceName, AdminId = MyInfo.Id, CreateDate = DateTime.Now, PriceUnit = "$", Currency = "USD", Price = 0, TaxPrice = 0, Moq = 1, FirstPic = ImgConfig.GetProductUrl(firstPic) }; if (Bll.BllProduct_Info.Insert(model) > 0) { succuessCount++; } } } if (succuessCount > 0) { return(MessageBoxAndReturn("导入成功!")); } else { return(MessageBoxAndReturn("导入失败,请联系管理员!")); } } catch (Exception ex) { Tools.Tool.LogHelper.WriteLog(typeof(ProductController), ex, MyInfo.Id, MyInfo.RealName); return(MessageBoxAndReturn("导入失败,请联系管理员!" + ex)); } }