There is no such property out of the box but the worksheet has a MergedCells
property with an array of all the merged cell addresses in the worksheet and a GetMergeCellId()
method which will give you the index for a given cell address.
We can therefore combine these into a little extension method you can use to get the address. Something like this:
public static string GetMergedRangeAddress(this ExcelRange @this)
{
if (@this.Merge)
{
var idx = @this.Worksheet.GetMergeCellId(@this.Start.Row, @this.Start.Column);
return @this.Worksheet.MergedCells[idx-1]; //the array is 0-indexed but the mergeId is 1-indexed...
}
else
{
return @this.Address;
}
}
which you can use as follows:
using (var excel = new ExcelPackage(new FileInfo("inputFile.xlsx")))
{
var ws = excel.Workbook.Worksheets["sheet1"];
var b3address = ws.Cells["B3"].GetMergedRangeAddress();
}
(Note that in the event that you use this method on a multi-celled range it will return the merged cell address for the first cell in the range only)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…