/// <summary> /// 导入总方法 /// </summary> public void Import(BizProduct bizProduct, BizSupplier bizSupplier) { //1 读取Excel列表 IList <Product> ProductsInExcel = ReadProductsFromExcel(CheckExcelFile()); //2 检查图片是否存在 CheckProductImages(ProductsInExcel); //检查数据库 if (NeedCheckWithDB) { //1 检查 供应商是否存在 IList <string> supplierNameList = GetSupplierNameList(ProductsInExcel); IList <string> supplierNameList_NotExists; IList <Supplier> supplierList = bizSupplier.GetListByNameList(supplierNameList, out supplierNameList_NotExists); if (supplierNameList_NotExists.Count > 0) { foreach (string supplierName in supplierNameList_NotExists) { sbImportMsg.AppendLine("不存在该供应商:" + supplierName); } return; } //2 检查数据是否已经导入 IList <Product> productsExisted; ProductsPassedDBCheck = bizProduct.CheckDB(ProductsHasImage, out productsExisted); ProductsExistedInDB = productsExisted; foreach (Product productExist in ProductsExistedInDB) { sbImportMsg.AppendLine("已存在该产品.供应商/型号:" + productExist.SupplierName + "/" + productExist.ModelNumber); } } //数据保存到数据库 // bizProduct.SaveList( }
/// <summary> /// 图片命名 /// </summary> /// <param name="supplierName"></param> /// <param name="productType"></param> /// <param name="ntsCode"></param> public void Excute(string filePath, string savePath) { BizProduct bizProduct = new BizProduct(); System.Collections.IList allPictures; string errMsg; IList <Product> products = bizProduct.ReadListFromExcelWithAllPictures( new System.IO.FileStream(filePath, System.IO.FileMode.Open) , out errMsg , out allPictures ); // IList<Product> products = importer.Read(new System.IO.FileStream(filePath, System.IO.FileMode.Open), out allPictures); if (products.Count != allPictures.Count) { throw new Exception(string.Format("提取失败:产品和图片的数量不相等.产品:{0},图片:{1}", products.Count, allPictures.Count)); } for (int i = 0; i < products.Count; i++) { HSSFPictureData pic = (HSSFPictureData)allPictures[i]; var modelNumber = NLibrary.StringHelper.ReplaceInvalidChaInFileName(products[i].ModelNumber, "$") + ".jpg"; string fileName = savePath + modelNumber; NLibrary.IOHelper.EnsureFileDirectory(fileName); File.WriteAllBytes(fileName, pic.Data); } }
/// <summary> /// 处理单个文件夹 检查 和 保存 /// </summary> /// <param name="folderPath">包含 excel和图片 文件夹路径</param> /// <param name="outSavePath"> 最终结果 保存路径. 合格数据, 多余图片,多余excel数据,数据库内已存在数据 </param> public void CheckAndSaveSingleFold(string folderPath, string outSavePath) { IList <Product> productsHasPicture, productsNotHasPicture, productsExistedInDB; IList <FileInfo> imagesHasProduct, imagesHasNotProduct; string folderName = folderPath; CheckSingleFolder(folderName, out productsHasPicture, out productsNotHasPicture, out productsExistedInDB, out imagesHasProduct , out imagesHasNotProduct); //将结果保存到数据库 BizProduct.SaveList(productsHasPicture); sbMsg.AppendLine(productsHasPicture.Count + ":已导入"); //结果保存到文件夹 DateTime beginSaveResultToDisk = DateTime.Now; string supplierName = string.Empty; if (productsExistedInDB.Count > 0) { supplierName = productsExistedInDB[0].SupplierName; } else if (productsHasPicture.Count > 0) { supplierName = productsHasPicture[0].SupplierName; } else if (productsNotHasPicture.Count > 0) { supplierName = productsNotHasPicture[0].SupplierName; } else { return; } supplierName = StringHelper.ReplaceInvalidChaInFileName(supplierName, string.Empty); HandlerCheckResult(supplierName, productsHasPicture , productsNotHasPicture , productsExistedInDB , imagesHasProduct , imagesHasNotProduct , outSavePath ); // sbMsg.AppendLine(productsHasPicture.Count+":有效图片数量" ); // if (productsHasPicture.Count > 0) { Result_ProductsSuccessImported = Result_ProductsSuccessImported.Concat(productsHasPicture).ToList(); } sbMsgForLog.AppendLine("成功导入数量:" + productsHasPicture.Count); sbMsgForLog.AppendLine("没有图片的产品数量:" + productsNotHasPicture.Count); sbMsgForLog.AppendLine("数据库里已存在的产品数量:" + productsExistedInDB.Count); Console.WriteLine("Time Cost beginSaveResultToDisk:" + (DateTime.Now - beginSaveResultToDisk).TotalSeconds); }
/// <summary> /// 文件结构检查 /// 正确结构: Folder-| /// -xls文件.xls /// -图片文件夹 /// excel读取为 /// </summary> /// <param name="folderPath"></param> /// <param name="productsHasPicture"></param> /// <param name="productsNotHasPicture"></param> /// <param name="imagesHasProduct"></param> /// <param name="imagesHasNotProduct"></param> public void CheckSingleFolder(string folderPath , out IList <Product> productsHasPicture , out IList <Product> productsNotHasPicture , out IList <Product> productsExistedInDB , out IList <FileInfo> imagesHasProduct , out IList <FileInfo> imagesHasNotProduct) { DirectoryInfo dir = new DirectoryInfo(folderPath); FileInfo[] excelFiles = dir.GetFiles("*.xls", SearchOption.TopDirectoryOnly); if (excelFiles.Length != 1) { throw new Exception("错误,文件夹 " + folderPath + " 应该有且仅有一个excel文件"); } FileInfo excelFile = excelFiles[0]; Stream stream = new FileStream(excelFile.FullName, FileMode.Open); IDataTableConverter <Product> productReader = new ProductDataTableConverter(); string errMsg; DataTable dt = new NLibrary.ReadExcelToDataTable(stream).Read(out errMsg); IList <Product> products = productReader.Convert(dt); sbMsg.AppendLine(products.Count + ":待导入"); sbMsgForLog.AppendLine("待导入产品数量:" + products.Count); //排除数据库内重复的数据 IList <Product> validItems = products; productsExistedInDB = new List <Product>(); if (CheckWithDatabase) { DateTime beginCheckDbExists = DateTime.Now; validItems = BizProduct.CheckDB( products, out productsExistedInDB); Console.WriteLine("Time Cost CheckDB:" + (DateTime.Now - beginCheckDbExists).TotalSeconds); sbMsg.AppendLine(productsExistedInDB.Count + ":已存在"); foreach (Product pi in productsExistedInDB) { sbMsg.AppendLine(" 名称:" + pi.Name + "型号:" + pi.ModelNumber + "供应商名称:" + pi.SupplierName); } } // DateTime beginCheckImage = DateTime.Now; CheckProductImages(validItems, folderPath, out productsHasPicture , out productsNotHasPicture , out imagesHasProduct , out imagesHasNotProduct); Console.WriteLine("Time Cost CheckImage:" + (DateTime.Now - beginCheckImage).TotalSeconds); sbMsg.AppendLine(productsNotHasPicture.Count + ":无图片"); foreach (Product pnopic in productsNotHasPicture) { sbMsg.AppendLine(" 名称:" + pnopic.Name + "型号:" + pnopic.ModelNumber + "供应商名称:" + pnopic.SupplierName); } }
public Product Populate(string strProperties, BizProduct bizProduct) { string[] pros = strProperties.Split(new string[] { "---" }, StringSplitOptions.None); if (pros.Length != 2) { throw new Exception("格式有误:" + strProperties); } Product p = bizProduct.GetOneBySupplierNameModelNumber(pros[0].Trim(), pros[1].Trim()); return(p); }
public void ImportWithDBCheck(BizProduct bizProduct, BizSupplier bizSupplier) { }