复制Excel的范围

介绍

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

使用Aspose.Cells复制范围

Aspose.Cells提供了一些重载Range.Copy方法来复制范围。 还有Range.CopyStyle只复制范围的样式;Range.CopyData只复制范围的值。

复制范围

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

查看以下代码:

// 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");
// Create target range of cells.
Range targetRange = worksheet.Cells.CreateRange("B1", "B2");
// Copy source range to target range in the same workhseet
targetRange.Copy(sourceRange);
// Create target range of cells.
workbook.Worksheets.Add();
worksheet = workbook.Worksheets[1];
targetRange = worksheet.Cells.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.Worksheets[0];
targetRange = worksheet.Cells.CreateRange("A1", "A2");
// Copy source range to target range in another workbook
targetRange.Copy(sourceRange);
view raw Copy-Range.cs hosted with ❤ by GitHub

使用选项粘贴范围

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");
// Create target range of cells.
Range targetRange = worksheet.Cells.CreateRange("B1", "B2");
// Init paste options.
PasteOptions options = new PasteOptions();
// Set paste type.
options.PasteType = PasteType.ValuesAndFormats;
options.SkipBlanks = true;
// Copy source range to target range
targetRange.Copy(sourceRange, options);
view raw Paste-Range.cs hosted with ❤ by GitHub

仅复制数据的范围

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

// 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");
// Create target range of cells.
Range targetRange = worksheet.Cells.CreateRange("B1", "B2");
// Copy the data of source range to target range
targetRange.CopyData(sourceRange);

高级主题