Reputation: 914
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 ?
Upvotes: 3
Views: 2269
Reputation: 12157
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.
Upvotes: 1
Reputation: 56735
Assuming the you use a method that can invoke & use the Excel object model, you check the MergeArea property of a cell to see if it contains anything other than that cell. If it does, then that cell is part of a MergeArea. Here's how I've done it in VBA:
IF CurrCell.MergeArea.Rows.Count > 1 Or CurrCell.MergeArea.Columns.Count > 1 Then
'CurrCell is part of a MergeArea... '
The equivalent C# VSTO code should be fairly similar.
Upvotes: 0