Is there an easy way to copy a style?

Is there an easy way to copy a style if I create a style, and I need another version of that style that is exactly the same, with one minor change? (Like bolding the font, or changing the foreground color.) I have not found an easy way of copying a style other than creating a new style and then writing a line of code for copying each property. For example if I add a new style like newStyle, and want it to be a copy of originalStyle for every style property I would need a line like the following.

newStyle.Font.Color = originalStyle.Font.Color;

If I just tried the following:
Style newStyle = originalStyle;
And then made changes to newStyle, original style would be changed as well.

Is there an easy way to copy a style without having to write a line of code to copy each property and still create 2 seperate styles that can be modified without affecting each other?

Much Appreciated,

Glenn Engelbart

Dear Glenn,

Thanks for your consideration.

I plan to add a Copy method to Style class at the end of this week. Please wait for about 2 days.

Dear Glenn,

The Hot Fix 1.5.10 is released. Please download it.

The Style.Copy method is added. You can refer to https://forum.aspose.com/t/119949

Aspose.Excel.Style style1 = excel.Styles[excel.Styles.Add()];
style1.ForegroundColor = Color.Red;
style1.Font.Name = “Times New Roman”;
style1.Font.Size = 14;
style1.HorizontalAlignment = TextAlignmentType.Center;

Aspose.Excel.Style style2 = excel.Styles[excel.Styles.Add()];

style2.Copy(style1);

style2.Font.Name = “Courier New”;
style2.HorizontalAlignment = TextAlignmentType.Right;
style2.ForegroundColor = Color.Yellow;

Thanks much for adding the method. There still is one problem. It isn’t copying BorderTypes: In my copy method I used the following code.

newStyle.Borders[BorderType.BottomBorder].LineStyle = originalStyle.Borders[BorderType.BottomBorder].LineStyle;
newStyle.Borders[BorderType.TopBorder].LineStyle = originalStyle.Borders[BorderType.TopBorder].LineStyle;
newStyle.Borders[BorderType.RightBorder].LineStyle = originalStyle.Borders[BorderType.RightBorder].LineStyle;
newStyle.Borders[BorderType.LeftBorder].LineStyle = originalStyle.Borders[BorderType.LeftBorder].LineStyle;

Is it possible to add borderTypes to the copy style method?

Dear Glenn,

Please download Fix 1.5.10.1.

Thanks for your suggestion.

@ihs,
We recommend you to use the latest version of the product for more exciting features. You can achieve your desired output using the following sample code with the latest version:

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

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

// Fill some sample data into the cells.
for (int i = 0; i < 50; i++)
{
    for (int j = 0; j < 10; j++)
    {
        cells[i, j].PutValue(i.ToString() + "," + j.ToString());
    }

}

// Create a range (A1:D3).
Range range = cells.CreateRange("A1", "D3");

// Create a style object.
Style style;
style = workbook.CreateStyle();
// Specify the font attribute.
style.Font.Name = "Calibri";
// Specify the shading color.
style.ForegroundColor = Color.Yellow;
style.Pattern = BackgroundType.Solid;
// Specify the border attributes.
style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
style.Borders[BorderType.TopBorder].Color = Color.Blue;
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
style.Borders[BorderType.BottomBorder].Color = Color.Blue;
style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
style.Borders[BorderType.LeftBorder].Color = Color.Blue;
style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
style.Borders[BorderType.RightBorder].Color = Color.Blue;
// Create the styleflag object.
StyleFlag flag1 = new StyleFlag();
// Implement font attribute
flag1.FontName = true;
// Implement the shading / fill color.
flag1.CellShading = true;
// Implment border attributes.
flag1.Borders = true;
// Set the Range style.
range.ApplyStyle(style, flag1);

// Create a second range (C10:E13).
Range range2 = cells.CreateRange("C10", "E13");

// Copy the range style only.
range2.CopyStyle(range);

dataDir = dataDir + "copyrangestyle.out.xls";
// Save the excel file.
workbook.Save(dataDir);

For more details have a look at the following article:
Copy Range Style Only
Copy Range Data with Style
Reusing Style Objects

Download the latest version of Aspose.Cells for .NET from the following link:
Aspose.Cells for .NET (Latest Version)

You can download the latest demos here.