Document Solutions for Excel, .NET Edition | Document Solutions
Features / Worksheet / Range Operations / Get Intersection, Union and Offset Range
In This Topic
    Get Intersection, Union and Offset Range
    In This Topic

    DsExcel allows you to get the intersection, union and offset of specified ranges using the IRange interface.

    To get the intersection and union of two or more ranges, you can use Intersect method and Union method of the IRange interface respectively. Similarly, the interface also provides Offset method to get the offset of a specified range.

    The sample code below shows how to get the intersection, union and offset of different ranges:

    C#
    Copy Code
    // Set the intersection of two range value and style.
    var intersectRange = worksheet.Range["A2:E6"].Intersect(worksheet.Range["C4:G8"]);
    intersectRange.Interior.Color = Color.FromArgb(56, 93, 171);
    intersectRange.Merge();
    intersectRange.Value = "Intersect Range";
    intersectRange.Font.Bold = true;
    intersectRange.Font.Color = Color.FromArgb(226, 231, 243);
    intersectRange.HorizontalAlignment = HorizontalAlignment.Center;
    intersectRange.VerticalAlignment = VerticalAlignment.Center;

     

    C#
    Copy Code
    // Set the union of two range value and font style.
    var unionRange = worksheet.Range["A11:D13"].Union(worksheet.Range["D14:G16"]);
    unionRange.Value = "Union Range";
    unionRange.Font.Bold = true;
    unionRange.Font.Color = Color.FromArgb(226, 231, 243);

     

    C#
    Copy Code
    // Set the offset of the range value and style.
    var offsetRange = worksheet.Range["B2:D4"].Offset(4, 4);
    offsetRange.Merge();
    offsetRange.Value = "Offset Range";

    Note: An exception is thrown, if one or more ranges from a different worksheet are specified or the offset set in Offset method is out of bounds.

    To view the code in action, see Intersection and Union, and Offset Demo sample.