I'm trying to clone a cellstyle using CloneStyleFrom however, it doesn't appear to be working as expected.
When I modify the 'cloned' style, the original cell style is also modified.
FYI - I'm using Version 2.1.3.0
Other than that - excellent work and thanks!
Code snippet...
```
private void Test()
{
IWorkbook workbook = new XSSFWorkbook();
sheet = workbook.CreateSheet("Sheet A1");
ICell cell = sheet.CreateRow(0).CreateCell(0);
cell.SetCellValue(1.00);
ICellStyle cellStyle = workbook.CreateCellStyle();
IDataFormat fmt = workbook.CreateDataFormat();
short xx = fmt.GetFormat("0.00");
cellStyle.DataFormat = xx;
cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thick;
cellStyle.BottomBorderColor = 8;
cell.CellStyle = cellStyle;
ICellStyle cellStyle2 = workbook.CreateCellStyle();
cellStyle2.CloneStyleFrom(cellStyle);
//Below change also affects cellStyle...
cellStyle2.BorderBottom = NPOI.SS.UserModel.BorderStyle.None;
FileStream sw = File.Create("cellStyler.xlsx");
workbook.Write(sw);
sw.Close();
}
```
Comments: ** Comment from web user: PETER_DA **
This is a tricky one. I have encountered that before and I am in the process of fixing / creating a workaround.
Problem is that "CloneStyleFrom" creates a shallow copy of the CellStyle. It will still link to the Border and Fill Xf Objects instead of copying those objects, too. When changing those values in the clone, they will affect all styles that reference them.
For instance. Imagine a green cell (foreground color, solid).
The cell style will link to a StylesTable fill (e.g. idx 1). The cloned cell style references the same index.
When working on the clone, it will affect the fill in the styles table instead of cloning it.
See public CT_Fill GetCTFill() in XSSFCellStyle.cs
It will only return a new instance when no fill is applied!
Also, PutBorder() and PutFill() functions in StylesTable.cs will only add changed Borders or Fills.
Trying to feed them copies of the Fills or Borders will not work.
I'll post my solution as soon as I have one.