public void ExportProductsToExcelFilesByBrands(string brand) { List <ExcelProduct> excelProducts = new List <ExcelProduct>(); List <Product> brandProducts = products.Where(p => p.ProductManufacturerMapping?.FirstOrDefault()?.Manufacturer?.Name == brand).ToList(); foreach (Product product in brandProducts) { log.Info("Product " + product.Sku + "is being processed "); ExcelProduct excelProduct = ExcelProductUtil.GenerateExcelProductFromProduct(product); excelProducts.Add(excelProduct); } _excelProductService.GenerateExcelSheetfromExcelProductsByBrand(excelProducts, brand); }
public static void CreateExcelSheet(string brand, List <ExcelProduct> excelProducts) { DataTable dtCodes = new DataTable(); dtCodes.Clear(); foreach (SpecificationAttribute specificationAttribute in SpecificationAttributeOptionUtil.specificationAttributes) { dtCodes.Columns.Add(specificationAttribute.Name); } foreach (ExcelProduct excelProduct in excelProducts) { DataRow dr = dtCodes.NewRow(); foreach (SpecificationAttribute specificationAttribute in SpecificationAttributeOptionUtil.specificationAttributes) { dr[specificationAttribute.Name] = ExcelProductUtil.FindSpecificationAttributeValue(specificationAttribute.Name, excelProduct); } dtCodes.Rows.Add(dr); } FileInfo file = new FileInfo(GenerateExcelFileName()); if (file.Exists) { ExcelPackage pck = new ExcelPackage(file); ExcelWorksheet ws = null; ws = pck.Workbook.Worksheets.Add(brand); ws.Cells["A1"].LoadFromDataTable(dtCodes, true); ws.Row(1).Style.Font.Bold = true; ws.Row(1).Style.Fill.PatternType = ExcelFillStyle.Solid; ws.Row(1).Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Yellow); pck.Save(); } else { ExcelPackage pck = new ExcelPackage(); ExcelWorksheet ws = null; ws = pck.Workbook.Worksheets.Add(brand); ws.Cells["A1"].LoadFromDataTable(dtCodes, true); ws.Row(1).Style.Font.Bold = true; ws.Row(1).Style.Fill.PatternType = ExcelFillStyle.Solid; ws.Row(1).Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Yellow); File.WriteAllBytes(GenerateExcelFileName(), pck.GetAsByteArray()); } }