//create a new workbook var workbook = new GrapeCity.Documents.Excel.Workbook(); IWorksheet worksheet = workbook.Worksheets[0]; worksheet.Range["A1:G1"].Merge(); worksheet.Range["A1:G1"].Value = "Intersect"; worksheet.Range["A1:G1"].HorizontalAlignment = HorizontalAlignment.Center; worksheet.Range["A2:E6"].Interior.Color = Color.FromArgb(145, 167, 214); worksheet.Range["C4:G8"].Interior.Color = Color.FromArgb(145, 167, 214); // 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; worksheet.Range["A10:G10"].Merge(); worksheet.Range["A10:G10"].Value = "Union"; worksheet.Range["A10:G10"].HorizontalAlignment = HorizontalAlignment.Center; worksheet.Range["A10:G10"].VerticalAlignment = VerticalAlignment.Center; var firstUnionRange = worksheet.Range["A11:D13"]; firstUnionRange.Merge(); firstUnionRange.Interior.Color = Color.FromArgb(56, 93, 171); firstUnionRange.HorizontalAlignment = HorizontalAlignment.Center; firstUnionRange.VerticalAlignment = VerticalAlignment.Center; var secondUnionRange = worksheet.Range["D14:G16"]; secondUnionRange.Merge(); secondUnionRange.Interior.Color = Color.FromArgb(145, 167, 214); secondUnionRange.HorizontalAlignment = HorizontalAlignment.Center; secondUnionRange.VerticalAlignment = VerticalAlignment.Center; // 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);
' Create a new Workbook Dim workbook As New Workbook Dim Worksheet As IWorksheet = workbook.Worksheets(0) With Worksheet.Range("A1:G1") .Merge() .Value = "Intersect" .HorizontalAlignment = HorizontalAlignment.Center End With Worksheet.Range("A2:E6").Interior.Color = Color.FromArgb(145, 167, 214) Worksheet.Range("C4:G8").Interior.Color = Color.FromArgb(145, 167, 214) ' Set the intersection of two range value And style. With Worksheet.Range("A2:E6").Intersect(Worksheet.Range("C4:G8")) .Interior.Color = Color.FromArgb(56, 93, 171) .Merge() .Value = "Intersect Range" .Font.Bold = True .Font.Color = Color.FromArgb(226, 231, 243) .HorizontalAlignment = HorizontalAlignment.Center .VerticalAlignment = VerticalAlignment.Center End With With Worksheet.Range("A10:G10") .Merge() .Value = "Union" .HorizontalAlignment = HorizontalAlignment.Center .VerticalAlignment = VerticalAlignment.Center End With With Worksheet.Range("A11:D13") .Merge() .Interior.Color = Color.FromArgb(56, 93, 171) .HorizontalAlignment = HorizontalAlignment.Center .VerticalAlignment = VerticalAlignment.Center End With With Worksheet.Range("D14:G16") .Merge() .Interior.Color = Color.FromArgb(145, 167, 214) .HorizontalAlignment = HorizontalAlignment.Center .VerticalAlignment = VerticalAlignment.Center End With ' Set the union of two range value And font style. With Worksheet.Range("A11:D13").Union(Worksheet.Range("D14:G16")) .Value = "Union Range" .Font.Bold = True .Font.Color = Color.FromArgb(226, 231, 243) End With ' save to an excel file workbook.Save("RangeIntersectAndUnion.xlsx")