Search
Styling the Spreadsheet

Styling the spreadsheet is done through the Style property. This is a property on the Cell, CellRange, Column, ColumnRange, Row and RowRange objects. In short, you can style anything – from an individual small Cell to the whole Worksheet.

Styling the Sheet

In order to apply styling to all cells in a sheet get them as a range and apply the styling:

C#  Copy Code

//create a global styling for the whole sheet
var globalStyle = activeSheet.CellRanges[0, 0, activeSheet.Columns.Count - 1, activeSheet.Rows.Count - 1].Style;
globalStyle.FontName = "Malgun Gothic";
globalStyle.FontSize = 8;
globalStyle.HorizontalAlignment = MindFusion.Spreadsheet.Wpf.HorizontalAlignment.Center;

 

The Style property is an instance of the IStyle interface and it has a long list of properties, which you can use to customize the appearance of the cell(s): fonts, borders strokes, brushes, padding, text color, alignment and wrap.

Styling Cells and Cell Ranges

That’s how you style a single cell:

C#  Copy Code

activeSheet.Cells["A3"].Style.FontBold = true;
activeSheet.Cells["A3"].Style.Background = new SolidColorBrush(Color.FromRgb(240, 240, 240));
activeSheet.Cells["A3"].Style.BorderBottomBrush = new SolidColorBrush(Color.FromRgb(220, 220, 220));
activeSheet.Cells["A3"].Style.BorderRightBrush = new SolidColorBrush(Color.FromRgb(220, 220, 220));

 This sample draws a light gray background on cell A3 and adds a gray border to the bottom and right of the cell.

You can style a range of cells as well, the code below places a top border on cells A7 to G7:

C#  Copy Code

activeSheet.CellRanges["A7:G7"].Style.BorderTopBrush = Brushes.Gray;

Conditional Formats

You can apply a given styling to a cell only when a certain condition is met. This is done with the ConditionalFormats property of IStyle objects. The Cell, CellRange, Column, ColumnRange, Row and RowRange objects have their own ConditionalFormats property.

The following sample paints in red the background of those cells among the second to the fifth cell on the 6th row whose data is less than 35:

C#  Copy Code

 var salesStyle = activeSheet.CellRanges[1, 5, 4, 5].Style;
 
// Set a conditional format
var format1 = salesStyle.ConditionalFormats.Add();
format1.Type = ConditionalFormatType.CellValue;
format1.Operator = ComparisonOperator.LessThan;
format1.First = "35";
format1.Style.Background = new SolidColorBrush(Color.FromArgb(255, 255, 120, 85));
format1.Style.Background.Freeze();