how to detect merged cells in c# using MS interop excel
MergeCells
is not a cells function, it's range function, so instead of:
if (ws.Cells[strtRow, j].MergeCells)
you need:
_Excel.Range range = (_Excel.Range) ws.Cells[strtRow, j];
if(range.MergeCells) //returns true if cell is merged or false if its not
If you want to check if a Range
contains merged cells, then the MergeCells
property is what you're after.
If a range is merged, it will return true
. If a range contains merged cells (i.e. some are merged, some aren't), it will return DBNull.Value
.
So, this should work for your entire sheet:
object mergeCells = ws.UsedRange.MergeCells;
var containsMergedCells = mergeCells == DBNull.Value || (bool)mergeCells;