Example #1
0
        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());
            }
        }