# Get Intersection, Union and Offset Range

DsExcel Java is a MESCIUS Documents product that offers a comprehensive library to create, manipulate, convert, and share Microsoft Excel-compatible spreadsheets.

## Content

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](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/IRange.html#intersect) method and [union](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/IRange.html#union) method of the [IRange](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/IRange.html) interface respectively. Similarly, the interface also provides [offset](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/IRange.html#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:

![](https://cdn.mescius.io/document-site-files/images/dd59ea42-cd61-4fa6-a018-6231c2a9c598/images/intersection.png)

```Java
// 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);
```

![](https://cdn.mescius.io/document-site-files/images/dd59ea42-cd61-4fa6-a018-6231c2a9c598/images/union.png)

```Java
// 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));
```

![](https://cdn.mescius.io/document-site-files/images/dd59ea42-cd61-4fa6-a018-6231c2a9c598/images/offset.png)

```Java
// 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);
```

>type=note
> **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](https://developer.mescius.com/documents-api-excel-java/demos/rangeintersectandunion), and [Offset](https://developer.mescius.com/documents-api-excel-java/demos/rangeoffset) Demo sample.