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?

Thanks



Hi,


Please try our latest versions, e.g v6.x, 7.0 etc. You may also find our latest fix, v7.0.0.2:
https://forum.aspose.com/t/124367

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:
1)
Workbook wb = new Workbook();
wb.Worksheets.Clear();
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);

2)
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].SetStyle(wb.Styles[“Header”]);
ws.Cells[0,0].PutValue(“Hello, world”);
wb.Save(“d:\test\headerstyle.xls”);

Check some topics for your complete reference:
http://www.aspose.com/documentation/.net-components/aspose.cells-for-.net/approaches-to-format-data-in-cells.html
http://www.aspose.com/documentation/.net-components/aspose.cells-for-.net/reuse-of-style-objects.html
http://www.aspose.com/documentation/.net-components/aspose.cells-for-.net/modify-an-existing-style.html

Thank you.

Thanks
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.

Hi,


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.