Applying a style to a range of cells

I’m trying to apply a style (basically: a background color) to a range of cells.


I found this sample in the official documentation:
(http://www.aspose.com/docs/display/cellsnet/Formatting+a+Range+of+Cells)

Worksheet sheet = gridDesktop1.GetActiveWorksheet();
//Creating a CellRange object starting from “B7” to “E7”
CellRange range = new CellRange(6, 1, 6, 4);

//Accessing and setting Style attributes
Style style = new Style(this.gridDesktop1);
style.Color = Color.Yellow;

//Applying Style object on the range of cells
sheet.SetStyle(range, style);


My problems are:

* I don’t have a “CellRange” class anywhere in Aspose.Cells …
* My “Worksheet” doesn’t have a “SetStyle” overload that accepts a range …

So how do I define a range and set its background color to my choice?

Hi,


Well, the CellRange class is present in Aspose.Cells.GridDesktop namespace and not in Aspose.Cells namespace. If you are using GridDesktop control in your winform/windows application visually, the the example code will work fine. But, if you are using Aspose.Cells library to format a range of cells to save an Excel file on some path for your needs, please see the document for your reference:
http://www.aspose.com/docs/display/cellsnet/Named+Ranges


See the sample code using Aspose.Cells component:
//Instantiate a new Workbook.
Workbook workbook = new Workbook();

//Get the first worksheet in the book.
Worksheet WS = workbook.Worksheets[0];

//Create a range of cells.
Aspose.Cells.Range range = WS.Cells.CreateRange(1, 1, 1, 18);

//Name the range.
range.Name = “MyRange”;

//Declare a style object.
Style stl;

//Create/add the style object.
stl = workbook.Styles[workbook.Styles.Add()];

//Specify some Font settings.
stl.Font.Name = “Arial”;
stl.Font.IsBold = true;

//Set the font text color
stl.Font.Color = Color.Red;

//To Set the fill color of the range, you may use ForegroundColor with
//solid Pattern setting.
stl.ForegroundColor = Color.Yellow;
stl.Pattern = BackgroundType.Solid;

//Create a StyleFlag object.
StyleFlag flg = new StyleFlag();
//Make the corresponding attributes ON.
flg.Font = true;
flg.CellShading = true;

//Apply the style to the range.
range.ApplyStyle(stl,flg);

//Save the excel file.
workbook.Save(“d:\test\rangestyles.xls”);

Great - thanks for your help! Got it working now.


Marc

Hi,


Thanks and you got it right.

Feel free to contact us any time, if you need further help. We will help you soon.

Have a nice time!