Document Solutions for Excel, Java 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 Java 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:

    Java
    Copy Code
    // Set the intersection of two range value and style.
    IRange intersectRange = worksheet.getRange("A2:E6").intersect(worksheet.getRange("C4:G8"));
    intersectRange.getInterior().setColor(Color.FromArgb(56, 93, 171));
    intersectRange.merge();
    intersectRange.setValue("Intersect Range");
    intersectRange.getFont().setBold(true);
    intersectRange.getFont().setColor(Color.FromArgb(226, 231, 243));
    intersectRange.setHorizontalAlignment(HorizontalAlignment.Center);
    intersectRange.setVerticalAlignment(VerticalAlignment.Center);

    Java
    Copy Code
    // Set the union of two range value and font style.
    IRange unionRange = worksheet.getRange("A11:D13").union(worksheet.getRange("D14:G16"));
    unionRange.setValue("Union Range");
    unionRange.getFont().setBold(true);
    unionRange.getFont().setColor(Color.FromArgb(226, 231, 243));

    Java
    Copy Code
    // Set the offset of the range value and style.
    IRange offsetRange = worksheet.getRange("B2:D4").offset(4, 4);
    offsetRange.merge();
    offsetRange.setValue("Offset Range");
    offsetRange.getFont().setBold(true);
    offsetRange.getFont().setColor(Color.FromArgb(226, 231, 243));
    offsetRange.getInterior().setColor(Color.FromArgb(56, 93, 171));
    offsetRange.setHorizontalAlignment(HorizontalAlignment.Center);
    offsetRange.setVerticalAlignment(VerticalAlignment.Center);
    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.