This project has moved and is read-only. For the latest updates, please go here.

Styles

Jul 10, 2011 at 12:40 AM

What are the thoughts on making XLStyle public instead of internal?

In my opinion it'd make it easier to manage and change styles. Also it'd allow us to create our own themes. Right now you have to set each individual property for each cell or range to apply a style (or create helper methods). Instead it'd be great if we could inherit a style or create them once and set the style object throughout the creation of the spreadsheet.

For example:

// 1. Create a style (or create in a "theme" class)
XLStyle H1 = new XLStyle(null, null);
H1.Border.BottomBorder = XLBorderStyleValues.Thin;
H1.Border.TopBorder = XLBorderStyleValues.Thin;
H1.Border.LeftBorder = XLBorderStyleValues.Thin;
H1.Border.RightBorder = XLBorderStyleValues.Thin;
H1.Fill.BackgroundColor = XLColor.FromArgb(31, 73, 125);
H1.Font.FontColor = XLColor.White;
H1.Font.Bold = true;
H1.Font.FontName = "Calibri";
H1.Font.FontSize = 10;
H1.Alignment.Vertical = XLAlignmentVerticalValues.Center;

(snip)

// 2. Set the style
worksheet.Cell(row, column).SetValue(value);
worksheet.Cell(row, column).Style = H1;

Jul 10, 2011 at 4:48 AM

I have to disagree with you on this. I tried the same thing with XLFont but had to move away from it because in my opinion it was just too clunky and not in the spirit of how this library is designed.

The solution is to implement Excel Styles and/or themes, not to expose the XLStyle class.

In the meantime you could do one of the following:

            var wb = new XLWorkbook();
            var ws = wb.Worksheets.Add("Sheet1");

Interim solution 1 (works across sheets but creates a named range)

            ws.Range("A1:D5").AddToNamed("CellsInRed");
            ws.Cell("F1").AddToNamed("CellsInRed");

            wb.NamedRange("CellsInRed").Ranges.Style
                .Fill.SetBackgroundColor(XLColor.Red)
                .Font.SetBold();

Interim solution 2 (only works within one sheet)

 

            ws.Ranges("A1:D5, F1").Style
                .Fill.SetBackgroundColor(XLColor.Red)
                .Font.SetBold();

Interim solution 3 (RECOMMENDED: works across sheets and doesn't use named ranges)

            var redRanges = ws.Ranges("A1:D5");
            redRanges.Add(ws.Range("F1"));
            redRanges.Style
                .Fill.SetBackgroundColor(XLColor.Red)
                .Font.SetBold();

This way instead of setting the style of the cell/range you add it to the collection and then apply the style.

/Manuel