复制Excel的范围

介绍

在Excel中,您可以选择一个范围,复制该范围,然后以特定选项粘贴到同一工作表、其他工作表或其他文件。

使用Aspose.Cells复制范围

Aspose.Cells提供了一些重载Range.Copy方法来复制范围。

复制范围

创建两个范围:源范围、目标范围,然后使用Range.Copy方法将源范围复制到目标范围。

查看以下代码:

// 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");
// Input some data with some formats into a few cells in the range.
sourceRange.get(0, 0).putValue("Test");
sourceRange.get(1, 0).putValue("123");
// Create target range of cells.
Range targetRange = worksheet.getCells().createRange("B1", "B2");
// Copy source range to target range in the same workhseet
targetRange.copy(sourceRange);
// Create target range of cells.
workbook.getWorksheets().add();
worksheet = workbook.getWorksheets().get(1);
targetRange = worksheet.getCells().createRange("A1", "A2");
// Copy source range to target range in another workhseet
targetRange.copy(sourceRange);
//Copy to another workbook
Workbook anotherWorkbook = new Workbook();
worksheet = workbook.getWorksheets().get(0);
targetRange = worksheet.getCells().createRange("A1", "A2");
// Copy source range to target range in another workbook
targetRange.copy(sourceRange);
view raw Copy-Range.java hosted with ❤ by GitHub

使用选项粘贴范围

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");
// Input some data with some formats into a few cells in the range.
sourceRange.get(0, 0).putValue("Test");
sourceRange.get(1, 0).putValue("123");
// Create target range of cells.
Range targetRange = worksheet.getCells().createRange("B1", "B2");
// Init paste options.
PasteOptions options = new PasteOptions();
// Set paste type.
options.setPasteType(PasteType.VALUES_AND_FORMATS);
options.setSkipBlanks(true);
// Copy source range to target range
targetRange.copy(sourceRange, options);

仅复制范围的数据。

还可以使用Range.CopyData方法来复制数据,示例如下:

// 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");
// Create target range of cells.
Range targetRange = worksheet.getCells().createRange("B1", "B2");
//Only copy data the range.
targetRange.CopyData(sourceRange);