npoi
npoi copied to clipboard
Cell borders not working properly
NPOI Version
2.6.2
File Type
- [ X ] XLSX
Issue Description
When creating a border for a merged cell, some of the sides come out wrong. Here is an example:
rowIdx += 2;
currentRow = sheet1.CreateRow(rowIdx);
currentRow.CreateCell(13).SetCellValue("Saving Summary");
currentRow.GetCell(13).CellStyle = summarySavingsStyle;
currentRow.GetCell(13).CellStyle.BorderTop = BorderStyle.Thick;
currentRow.GetCell(13).CellStyle.BorderLeft = BorderStyle.Thick;
currentRow.GetCell(13).CellStyle.BorderRight = BorderStyle.None;
currentRow.GetCell(13).CellStyle.BorderBottom = BorderStyle.Thin;
currentRow.CreateCell(14);
currentRow.GetCell(14).CellStyle = summarySavingsStyle;
currentRow.GetCell(14).CellStyle.BorderTop = BorderStyle.Thick;
currentRow.GetCell(14).CellStyle.BorderLeft = BorderStyle.None;
currentRow.GetCell(14).CellStyle.BorderRight = BorderStyle.None;
currentRow.GetCell(14).CellStyle.BorderBottom = BorderStyle.Thin;
currentRow.CreateCell(15);
currentRow.GetCell(15).CellStyle = summarySavingsStyle;
currentRow.GetCell(15).CellStyle.BorderTop = BorderStyle.Thick;
currentRow.GetCell(15).CellStyle.BorderLeft = BorderStyle.None;
currentRow.GetCell(15).CellStyle.BorderRight = BorderStyle.Thick;
currentRow.GetCell(15).CellStyle.BorderBottom = BorderStyle.Thin;
CellRangeAddress region = new CellRangeAddress(rowIdx, rowIdx, 13, 15);
sheet1.AddMergedRegion(region);
In the code above you can see that I set a thick border for the following:
- Cell 13: top and left
- Cell 14: top
- Cell 15: top and right
Which is not reflected in the excel file:
If you wondering what's the style applied to the cells, this is the code:
XSSFCellStyle summarySavingsStyle = (XSSFCellStyle)workbook.CreateCellStyle();
summarySavingsStyle.SetFont(font);
summarySavingsStyle.Alignment = HorizontalAlignment.Center;
summarySavingsStyle.VerticalAlignment = VerticalAlignment.Center;
summarySavingsStyle.SetFillForegroundColor(new XSSFColor(new byte[] { 3, 252, 44 }));
summarySavingsStyle.FillPattern = FillPattern.SolidForeground;
Update:
So, I found out that you cannot assign a style to a cell and then, later on, add the borders, so for cells with the same style but with different borders, you will need to create a new style just with a different border.
Now that I figure that out, I manage to create the spreadsheet as I wanted it to be, but it's a bit annoying because there is a lot of duplicated code, for one table, I need to have a style for each corner, another one for the central of the table sides and finally another style for the core cells in the table.
Basically, to build a table I had to copy the same style 9 times just to adapt the borders.
I believe it's not a bug, you can clone the existing style with CloneStyleFrom
method and then change it.