How to merge cells in Excel?
I have a list of objects and I am trying to dedicate three rows for every object and I am using the "Office Open Xml library" with this routine:
int row = 1, col = 1;
for (int i = 0; i < MyList.Count * 3; i+=3)
{
MySheet.Cells[row + i, col, row + i + 1, col].Merge = true; // merge two and get a "result"
MySheet.Cells[row + i + 1, col, row + i + 2, col].Merge = true; // merge "result" with third row
}
However it pops an error saying can't merge already merged cell.
So the question is How to merge more than two cells in Excel?
The indexes specify a range of cells, not only two adjacent cells. To merge cells across three rows you can write
MySheet.Cells[row + i, col, row + i + 2, col].Merge = true;
Or you can merge a 3x2 block
MySheet.Cells[row + i, col, row + i + 2, col+1].Merge = true;
Everything in Excel is a range. Even a single cell is treated as a single-cell. This makes working with single cells a bit weird but it makes applying styles and formulas a lot easier