This project has moved. For the latest updates, please go here.

How to set background color based on template style

Jan 31, 2012 at 3:06 PM

I am trying to set the Fill property of an ExcelStyle based on another ExcelStyle object I'm using as a template.  Assigning the template Fill property to the new Fill property does not work

newStyle.Fill = templateStyle.Fill;

So I tried setting the individual properties of ExcelFill, but BackgroundColor and PatternColor are read-only, and there isn't a public constructor for ExcelFill.  So that leaves me setting the properties on ExcelColor, but again the properties are read-only and there isn't a public constructor.  There is a public method called SetColor() which takes a Color object that I thought would work, but I am not able to produce a Color object.  I thought I could use the Color.FromArgb() method(s), but I have found instances where the templateStyle.Fill.BackgroundColor.Rgb property is an empty string.  My question is how do I set the Fill.BackgroundColor property of the new style so that it matches the Fill.BackgroundColor property of my template style?

Jan 31, 2012 at 5:29 PM

Set the StyleID from the cells you want to copy from to the cells you want to. This will copy the entire style from the cell, not only the fill.