Search code examples
c#excelvstooledb

VSTO merged cells


Is there any way in VSTO (/JET OLEDB or other methods of reading excel files) to tell if the data comes from a single cell or merged range of cells and get this range ?


Solution

  • The shortest route here is to make use of the Boolean Range.MergeCells property.

    Assuming that your cell reference were named myCell, you could use something like:

    if (myCell.MergeCells)
    {
        // The 'myCell' is part of a merged cell area.
    }
    Else
    {
       // The 'myCell' is not part of any merged cell area.
    } 
    

    You could also check the Cells.Count on the Range returned by the Range.MergeArea property:

    if (myCell.MergeArea.Cells.Count > 1) {...} 
    

    or:

    if (myCell.MergeArea.Count > 1) {...}
    

    The last example works because the Range.Count property always returns the same value as does the Range.Cells.Count, by design.