插入范围

介绍

在 Excel 中,您可以选择一个范围,然后插入一个范围,并向右或向下移动其他数据。

移动选项

使用 Aspose.Cells 插入范围

Aspose.Cells 提供 Cells.InsertRange 方法来插入范围。

插入范围并向右移动单元格

插入一个范围,并像下面的 Aspose.Cells 代码一样向右移动单元格:

// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get all the worksheets in the book.
WorksheetCollection worksheets = workbook.getWorksheets();
// Get the first worksheet in the worksheets collection.
Worksheet worksheet = workbook.getWorksheets().get(0);
// Create a range of cells.
Range sourceRange = worksheet.getCells().createRange("A1", "A2");
// Set a few cells in the range.
sourceRange.get(0, 0).putValue("Test");
sourceRange.get(1, 0).putValue("123");
CellArea ca = CellArea.createCellArea("A1", "A2");
worksheet.getCells().insertRange(ca, ShiftType.RIGHT);
String b1Value = worksheet.getCells().get("B1").getStringValue();// "Test"

插入范围并向下移动单元格

插入一个范围,并像下面的 Aspose.Cells 代码一样向下移动单元格:

// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get all the worksheets in the book.
WorksheetCollection worksheets = workbook.getWorksheets();
// Get the first worksheet in the worksheets collection.
Worksheet worksheet = workbook.getWorksheets().get(0);
// Create a range of cells.
Range sourceRange = worksheet.getCells().createRange("A1", "A2");
// Set a few cells in the range.
sourceRange.get(0, 0).putValue("Test");
sourceRange.get(1, 0).putValue("123");
CellArea ca = CellArea.createCellArea("A1", "A2");
worksheet.getCells().insertRange(ca, ShiftType.DOWN);
String a3Value = worksheet.getCells().get("A3").getStringValue();// "Test"