Insert Ranges

Introduction

In Excel, you can select a range, then insert a range and shift other data right or down.

Shift options

Insert Ranges Using Aspose.Cells

Aspose.Cells provides Cells.InsertRange method to insert a range.

Insert Ranges And Shift Cells Right

Insert a ranage and shift cells right as the following codes with Aspose.Cells:

// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get all the worksheets in the book.
WorksheetCollection worksheets = workbook.Worksheets;
// Get the first worksheet in the worksheets collection.
Worksheet worksheet = workbook.Worksheets[0];
// Create a range of cells.
Range sourceRange = worksheet.Cells.CreateRange("A1", "A2");
// Input some data with some formattings into
// A few cells in the range.
sourceRange[0, 0].PutValue("Test");
sourceRange[1, 0].PutValue("123");
CellArea ca = CellArea.CreateCellArea("A1", "A2");
worksheet.Cells.InsertRange(ca, ShiftType.Right);
Console.WriteLine(worksheet.Cells["B1"].StringValue == "Test");

Insert Ranges And Shift Cells Down

Insert a ranage and shift cells down as the following codes with Aspose.Cells:

// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get all the worksheets in the book.
WorksheetCollection worksheets = workbook.Worksheets;
// Get the first worksheet in the worksheets collection.
Worksheet worksheet = workbook.Worksheets[0];
// Create a range of cells.
Range sourceRange = worksheet.Cells.CreateRange("A1", "A2");
// Input some data with some formattings into
// A few cells in the range.
sourceRange[0, 0].PutValue("Test");
sourceRange[1, 0].PutValue("123");
CellArea ca = CellArea.CreateCellArea("A1", "A2");
worksheet.Cells.InsertRange(ca, ShiftType.Down);
Console.WriteLine(worksheet.Cells["A3"].StringValue == "Test");