public Stylesheet CreateStylesheet(List <ExcelSheet> sheets) { var formats = new CellFormats(); var allStyles = new List <ExcelCellStyle>(); foreach (var item in sheets.SelectMany(s => s.Cells)) { if (item.Value.CellStyle != null) { allStyles.Add(item.Value.CellStyle); } } var distinctStyles = allStyles.Distinct().ToList(); foreach (var item in distinctStyles) { var formatIndex = (uint)formats.Count(); foreach (var style in allStyles.Where(m => m.Equals(item))) { style.SetStyleIndex(formatIndex); } var cellFormat = new CellFormat(); this.AddNumberFormatToCellFormat(item, cellFormat); this.AddBackgroundToCellFormat(item, cellFormat); this.AddFontToCellFormat(item, cellFormat); this.AddAlignmentToCellFormat(item, cellFormat); this.AddBordersToCellFormat(item, cellFormat); formats.AppendChild(cellFormat); } var styleSheet = new Stylesheet { Fonts = new Fonts(this.fonts), Fills = new Fills(this.fills), Borders = new Borders(this.borders), CellStyleFormats = new CellStyleFormats(new CellFormat()), CellFormats = formats }; return(styleSheet); }