private IList <Product> ReadProductsFromExcel(Stream stream) { IDataTableConverter <Product> productReader = new ProductDataTableConverter(); string errMsg; DataTable dt = new NLibrary.ReadExcelToDataTable(stream).Read(out errMsg); IList <Product> products = productReader.Convert(dt); return(products); }
/// <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); } }