Search code examples
c#epplusepplus-4

How can I partially change the border of a merged cell?


So given this code:

var mergeCells = worksheet.Cells["A1:B5"];
mergeCells.Merge = true;
mergeCells.Style.Border.BorderAround(ExcelBorderStyle.Medium);

var notWorkingCell = worksheet.Cells["C1"];
notWorkingCell.Style.Border.Right.Style = ExcelBorderStyle.Medium;
notWorkingCell.Style.Border.Left.Style = ExcelBorderStyle.None; // <--This does not happen

I would expect that the left border arround notWorkingCell (C1) would be removed. This is not happening:

Result of above code

How can I partially change the border of a merged cell? With pure Excel it is possible.


Solution

  • Looks like the border styles need to be explicitly set on the cell address within the merged cell itself, not the ExcelRange:

    var mergeCells = worksheet.Cells["A1:B5"];
    mergeCells.Merge = true;
    mergeCells.Style.Border.BorderAround(ExcelBorderStyle.Medium);
    
    var notWorkingCell = worksheet.Cells["C1"];
    notWorkingCell.Style.Border.Right.Style = ExcelBorderStyle.Medium;
    
    var cellToLeft = notWorkingCell.Start;
    worksheet.Cells[cellToLeft.Row, cellToLeft.Column - 1]
        .Style.Border.Right.Style = ExcelBorderStyle.None;
    

    enter image description here