Skip to content
This repository has been archived by the owner on Mar 9, 2020. It is now read-only.

Formatting and styling

Jan Källman edited this page Sep 17, 2017 · 2 revisions

Cell styling is accessed by the Style property. You can easily style your spreadsheets by using the indexer of the Cells property decribed above. Lets say you want to set the numberformat of a range...

worksheet.Cells["C2:C5"].Style.Numberformat.Format = "#,##0";

...or you want to set the header row to bold and dark background and white font...

using (var range = worksheet.Cells[1, 1, 1, 5])  //Address "A1:A5"
{
    range.Style.Font.Bold = true;
    range.Style.Fill.PatternType = ExcelFillStyle.Solid;
    range.Style.Fill.BackgroundColor.SetColor(Color.DarkBlue);
    range.Style.Font.Color.SetColor(Color.White);
}

To see the some real code showing cell access and styling look into the sample project, for example sample 6

You can also create your own named styles, using the Workbook.Style.CreateNamedStyle method and the ExcelRange.StyleName property. As shown in sample 6...

            //Add a HyperLink to the statistics sheet. 
            var namedStyle = pck.Workbook.Styles.CreateNamedStyle("HyperLink");  
            namedStyle.Style.Font.UnderLine = true;
            namedStyle.Style.Font.Color.SetColor(Color.Blue);
            ws.Cells["K13"].Hyperlink = new ExcelHyperLink("Statistics!A1", "Statistics");
            ws.Cells["K13"].StyleName = "HyperLink";