// Create a new workbook Workbook workbook = new Workbook(); IWorksheet worksheet = workbook.getWorksheets().get(0); worksheet.getRange("A1:G1").merge(); worksheet.getRange("A1:G1").setValue("Intersect"); worksheet.getRange("A1:G1").setHorizontalAlignment(HorizontalAlignment.Center); worksheet.getRange("A2:E6").getInterior().setColor(Color.FromArgb(145, 167, 214)); worksheet.getRange("C4:G8").getInterior().setColor(Color.FromArgb(145, 167, 214)); // 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); worksheet.getRange("A10:G10").merge(); worksheet.getRange("A10:G10").setValue("Union"); worksheet.getRange("A10:G10").setHorizontalAlignment(HorizontalAlignment.Center); worksheet.getRange("A10:G10").setVerticalAlignment(VerticalAlignment.Center); IRange firstUnionRange = worksheet.getRange("A11:D13"); firstUnionRange.merge(); firstUnionRange.getInterior().setColor(Color.FromArgb(56, 93, 171)); firstUnionRange.setHorizontalAlignment(HorizontalAlignment.Center); firstUnionRange.setVerticalAlignment(VerticalAlignment.Center); IRange secondUnionRange = worksheet.getRange("D14:G16"); secondUnionRange.merge(); secondUnionRange.getInterior().setColor(Color.FromArgb(145, 167, 214)); secondUnionRange.setHorizontalAlignment(HorizontalAlignment.Center); secondUnionRange.setVerticalAlignment(VerticalAlignment.Center); // 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)); // Save to an excel file workbook.save("RangeIntersectAndUnion.xlsx");