插入范围
Contents
[
Hide
]
介绍
在 Excel 中,您可以选择一个范围,然后插入一个范围,并向右或向下移动其他数据。
使用 Aspose.Cells 插入范围
Aspose.Cells 提供 Cells.InsertRange 方法来插入范围。
插入范围并向右移动单元格
插入一个范围,并像下面的 Aspose.Cells 代码一样向右移动单元格:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); | |
插入范围并向下移动单元格
插入一个范围,并像下面的 Aspose.Cells 代码一样向下移动单元格:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); | |