Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cell borders not working properly #1248

Closed
joaopf-silva opened this issue Jan 8, 2024 · 2 comments
Closed

Cell borders not working properly #1248

joaopf-silva opened this issue Jan 8, 2024 · 2 comments
Labels

Comments

@joaopf-silva
Copy link

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:
image

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;
@joaopf-silva
Copy link
Author

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.

@Bykiev
Copy link
Collaborator

Bykiev commented Feb 9, 2024

I believe it's not a bug, you can clone the existing style with CloneStyleFrom method and then change it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants