How to apply outline table border to a cell range using OpenXml?

6.9k views Asked by At

I set border style for each cell. But if you look closely, you'll see the blue lines are not consecutive. There are some white hash marks across the vertical lines. Because inside the table, I set the top and bottom cell border as white. Anyone knows how to avoid this?

Table Cell

WorksheetPart v_worksheetPart = a_workbookPart.AddNewPart<WorksheetPart>();
v_worksheetPart.Worksheet = new Worksheet();
SheetData v_sheetData = new SheetData();
Row v_Row = new Row();
Cell v_Cell = new Cell();
...
v_Row.Append(v_Cell);
v_sheetData.Append(v_Row);
v_worksheetPart.Worksheet.AppendChild(v_sheetData);
...
4

There are 4 answers

2
Joyin On BEST ANSWER

Taterhead's solution is a nice one. I have another one here. Instead of drawing white borders, just set the background color of the cell range to white.

Thanks everyone's help!

5
napi15 On

Just call

ws.Cells[row, col].Style.Border.BorderAround(ExcelBorderStyle.Thin);
1
Taterhead On

Instead of drawing white borders to cover up the default grey grid lines, you should instead just hide the grid lines of your worksheet. To do this via code you would set the ShowGridLines property to False in the SheetView, like the code in this SO answer.

Then remove your code that added white borders to your solution and leave in the code for the blue borders. This will remove the white breaks in your blue border.

0
Taterhead On

Another solution would be to start with a worksheet where all the gridlines are colored white. The first bullet here shows you how to do this with Excel. To do this on a blank workbook, use the code below:

  sheetView1.DefaultGridColor = false;
  sheetView1.ColorId = 9U; 

According to the docs, these properties tell excel not to paint the gridlines the default color and use color index 9 instead. The 9 corresponds to the White color index in a blank excel workbook. It will make all cells have a white gridline color by default.

Then you would remove any of your existing code that painted any cell borders white and leave the code that painted you cell borders blue. This is another way to remove the white hash marks in your original problem.