I'm creating an excel xlsx file from scratch with NPOI in c#, and need to have specific cellstyles for each of my cells. But as far as i can tell, every time i change the cellstyle of one of my cells, it modifies another unrelated cell.
Each time I create a cell, i assign a Cellstyle created right before with my XSSFWorkbook.CreateCellStyle(). I assume it should be a particular cellstyle just for the cell. However i see that it's not true and it seems to be the same reference as cells created before or after. Despite me calling XSSFWorkbook.CreateCellStyle() and setting it for each cell i'm creating.
Here's how I create my cells :
for (var i = 0; i < nbCellules; i++)
{
var cell = row.CreateCell(i);
var style = xssfwb.CreateCellStyle();
cell.CellStyle = xssfwb.CreateCellStyle();
cell.CellStyle.BorderLeft = GetLeftBorderStyleFromIndex(i);
cell.CellStyle.BorderRight = GetRightBorderStyleFromIndex(i);
}
With that code, I do the following :
row.GetCell(0).CellStyle.BorderBottom = BorderStyle.Thick;
I think that only that particular cell should be impacted.
However, every row now has a thick bottom border aswell.
Does somebody knows where I'm wrong?
Ok, so it seems that I have understood what's going on.
Note that nobody has answered me whatsoever, so that is an empirical answer.
It seems that if you do this :
var cell1 = sheet.GetRow(0).CreateCell(0);
var style = workBook.CreateCellStyle();
style.BorderBottom = BorderStyle.Thick;
var cell2 = sheet.GetRow(1).CreateCell(0);
var style = workBook.CreateCellStyle();
style.BorderBottom = BorderStyle.Thick;
// Now if you decide to change something from the style of cell2
cell2.CellStyle.BorderRight = BorderStyle.Dotted;
// it seems that cell1 now has BorderStyle.Dotted pour son Cellstyle.BorderRight
I don't know exactly what's going on, but it seems that once a CellStyle is affected to a cell, if he's the same as another cell's CellStyle, then it's not cloned, but shared.
I then described every cellStyle before affecting them to a cell, and now it worked.
Feel free to contact me for more details!