Free Support Forum -

Set cell with a named style

Hi !
v4.8.1.0 / Aspose.Cells

I want to add more named styles in my workbook and then use them on a cell.

For add named styles in workbook i use:

Style namedStyle = workbook.Styles[wb.Styles.Add()];
namedStyle.Name = “MyStyle”;
namedStyle.ForegroundColor = Color.Red;

For set “MyStyle” on cell, I’d like to do something like that:
worksheet.Cells[0, 0].Style.Name = “MyStyle”;

But it doesn’t work !

This works,
but it requires reference of workbook, (and heavy memory?)
worksheet.Cells[0, 0].Style = workbook.Styles[“MyStyle”];

What’s the best practice?



Please try our latest versions, e.g v6.x, 7.0 etc. You may also find our latest fix, v7.0.0.2:

We changed the internal model for Style/Formatting. Now it is efficient model. We introduced new Cell.GetStyle/SetStyle model that takes very less memory and is efficient regarding performance. We have eliminated older Cell.Style property that was consuming lot of memory in the long run.

See some code segments for reference:
Workbook wb = new Workbook();
Worksheet ws = wb.Worksheets.Add(“New”);
Style style = wb.Styles[wb.Styles.Add()];
style.Name = “DateTimeStyle”;
style.Custom = “dd-mm hh:mm:ss”;
DateTime dt = DateTime.Now;
ws.Cells[0, 0].PutValue(dt);
ws.Cells[0, 0].SetStyle(style);

Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook();
Aspose.Cells.Style style = wb.Styles[wb.Styles.Add()];
style.Name = “Header”;
style.ForegroundColor = System.Drawing.Color.Red;
style.Pattern = BackgroundType.Solid;
Aspose.Cells.Worksheet ws = wb.Worksheets[0];
ws.Cells[0,0].PutValue(“Hello, world”);

Check some topics for your complete reference:

Thank you.

So the solution I propose, as appropriate:
worksheet.Cells[0, 0].Style = workbook.Styles[“MyStyle”];

I am forced to have a reference to the workbook for set the cell style.


Well, if you want to stick with the older version, I am afraid you have to use your approach which is fine too.

Thank you.