Set the style for a cell

Hi,


I am trying to set the style for the cell and it doesn’t seem to work. This is the sample I have taken from the documentation and trying to work on. But the sample doesn’t seem to work. Just wanted to make sure I am doing the right thing. I am using Aspose.Cells version 7.3.3.0

//Create a workbook.
Workbook workbook = new Workbook();

//Create a new style object.
Style style = workbook.CreateStyle();

//Set the number format.
style.Number = 14;

//Set the font color to red color.
style.Font.Color = System.Drawing.Color.Red;

//Name the style.
style.Name = “Date1”;

Worksheet ws = workbook.Worksheets[0];
//Get the first worksheet cells.
Aspose.Cells.Cells cells = ws.Cells;

//Specify the style (described above) to A1 cell.
cells[“A1”].SetStyle(style);

//Create a range (B1:D1).
Range range = cells.CreateRange(“B1”, “D1”);

//Initialize styleflag object.
StyleFlag flag = new StyleFlag();

//Set all formatting attributes on.
flag.All = true;

//Apply the style (described above)to the range.
range.ApplyStyle(style, flag);

//Modify the style (described above) and change the font color from red to black.
style.Font.Color = System.Drawing.Color.Black;

//Done! Since the named style (described above) has been set to a cell and range,
//the change would be Reflected(new modification is implemented) to cell(A1) and //range (B1:D1).
style.Update();

//Save the excel file.
for (int i = 2; i <= 10;i++ )
{
ws.Cells[3, i].PutValue(“HHello”);
}
//Saving the Excel file
workbook.Save(“C:\book1.xls”);

Was able to figure that out. This sample worked as expected.


Format Worksheet Cells in a Workbook|Documentation

Hi,

Thanks for your update and using Aspose.Cells.

It is good to know that your issue is resolved with the above article sample code. Let us know if you encounter any other issue, we will look into it and help you further.

Please note, Style.Update() method is valid for named styles only. It is not for unnamed styles.

Here is the description of this method

Apply the named style to the styles of the cells which use this named style. It works like clicking the “ok” button after you finished modifying the style. Only applies for named style.