/// <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> /// 文件结构检查 /// 正确结构: 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); } }