Apply Style To Discontinuous Cell

Hi ,

I am using "Range" to apply style to continuous cells(horizontally and vertically).But i need to apply style to random cell . Is there any way i can apply style for more than one cell in single shot(similar to range for continuous cell).

Eg: If we want to apply same style for cell : A1 , A2 , A3

Then we can create a Range Range range = wrkSheet.Cells.CreateRange("A1", "A3");

Then we can apply style to this range.

But if we need to apply same style to A1 , A7 ,B9 ,C10 (random values - one can choose any set of discontinuous cell values) : How Can this be done ?

Hi,


It would be better if you could apply to style to individual cells only in your desired range(s) by using Cell.SetStyle() or Cell.ApplyStyle() method. Anyways, please see the following sample code that denotes your desired approach to somewhat for your reference for your requirements.
e.g
Sample code:

Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];

Cells cells = sheet.Cells;


NameCollection names = workbook.Worksheets.Names;
Name name = names[names.Add(“Myrange1”)];
name.RefersTo = “=Sheet1!A1, Sheet1!A7, Sheet1!B9, Sheet1!C10:C20”;

Style style = workbook.Styles[workbook.Styles.Add()];
style.ForegroundColor = Color.Yellow;
style.Pattern = BackgroundType.Solid;
StyleFlag flag = new StyleFlag();
flag.CellShading = true;


Range[] rngs = name.GetRanges();
if (rngs != null)
{

for (int i = 0; i < rngs.Length; i++)
{
rngs[i].ApplyStyle(style, flag);
}
}


workbook.Save(“e:\test2\outrange_style_book1.xls”);

Hope, this helps a bit.

Thank you.