复制行和列

介绍

有时,您需要复制工作表中的行和列而不复制整个工作表。使用 Aspose.Cells,可以在工作簿内或工作簿之间复制行和列。

复制行(或列)时,其中包含的数据,包括公式(具有更新的引用)和值、注释、格式、隐藏的单元格、图像和其他绘图对象也会被复制。

使用 Microsoft Excel 复制行和列

  1. 选择要复制的行或列。
  2. 要复制行或列,请单击复制标准工具栏,或按CTRL键+C
  3. 选择要复制选择的位置下方或右侧的行或列。
  4. 复制行或列时,单击已复制 Cells插入菜单。

复制单行

Aspose.Cells 提供了[复制行](https://reference.aspose.com/cells/java/com.aspose.cells/cells#copyRow(com.aspose.cells.Cells,%20int,%20int) 的方法Cells班级。此方法将所有类型的数据(包括公式、值、注释、单元格格式、隐藏单元格、图像和其他绘图对象)从源行复制到目标行。

这[复制行](https://reference.aspose.com/cells/java/com.aspose.cells/cells#copyRow(com.aspose.cells.Cells,%20int,%20int)方法采用以下参数:

  • 来源Cells目的,
  • 源行索引,和
  • 目标行索引。

使用此方法复制工作表中的一行,或复制到另一个工作表。这[复制行](https://reference.aspose.com/cells/java/com.aspose.cells/cells#copyRow(com.aspose.cells.Cells,%20int,%20int)方法的工作方式与 Microsoft Excel 类似。因此,例如,您不需要明确设置目标行的高度,该值也会被复制。

下面的示例演示如何复制工作表中的一行。它使用模板 Microsoft Excel 文件并复制第二行(包含数据、格式、注释、图像等)并将其粘贴到同一工作表中的第 12 行。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(CopyingRows.class) + "rows_cloumns/";
// Create a new Workbook.
Workbook excelWorkbook = new Workbook(dataDir + "book1.xls");
// Get the first worksheet in the workbook.
Worksheet wsTemplate = excelWorkbook.getWorksheets().get(0);
// Copy the second row with data, formating, images and drawing objects to the 12th row in the worksheet.
wsTemplate.getCells().copyRow(wsTemplate.getCells(), 2, 10);
// Save the excel file.
excelWorkbook.save(dataDir + "CopyingRows_out.xls");
// Print message
System.out.println("Row and Column copied successfully.");

执行以下代码时会生成以下输出。

该行以最高的精度和准确度复制

待办事项:图片_替代_文本

复制多行

您还可以在使用[Cells.copyRows](https://reference.aspose.com/cells/java/com.aspose.cells/cells#copyRow(com.aspose.cells.Cells,%20int,%20int)方法,它采用整数类型的附加参数来指定要复制的源行数。

下面是包含 3 行数据的输入电子表格的快照,而下面提供的代码片段将所有 3 行复制到从第 7 行开始的新位置。

待办事项:图片_替代_文本

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(CopyingMultipleRows.class);
// Create an instance of Workbook class by loading the existing spreadsheet
Workbook workbook = new Workbook(dataDir + "aspose-sample.xlsx");
// Get the cells collection of worksheet by name Rows
Cells cells = workbook.getWorksheets().get("Rows").getCells();
// Copy the first 3 rows to 7th row
cells.copyRows(cells, 0, 6, 3);
// Save the result on disc
workbook.save(dataDir + "output.xlsx");

这是执行上述代码片段后生成的电子表格视图。

待办事项:图片_替代_文本

复制单列

Aspose.Cells 提供了[复制列](https://reference.aspose.com/cells/java/com.aspose.cells/cells#copyColumn(com.aspose.cells.Cells,%20int,%20int) 的方法Cells类,此方法将所有类型的数据,包括公式(具有更新的引用)和值、注释、单元格格式、隐藏单元格、图像和其他绘图对象从源列复制到目标列。

这[复制列](https://reference.aspose.com/cells/java/com.aspose.cells/cells#copyColumn(com.aspose.cells.Cells,%20int,%20int)方法采用以下参数:

  • 来源Cells目的,
  • 源列索引,和
  • 目标列索引。

使用复制列 方法复制工作表中的列或复制到另一个工作表。

本示例从工作表复制一列并将其粘贴到另一个工作簿的工作表中。

列从一个工作簿复制到另一个工作簿

待办事项:图片_替代_文本

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(CopyingColumns.class) + "rows_cloumns/";
// Create a new Workbook.
Workbook excelWorkbook = new Workbook(dataDir + "book1.xls");
// Get the first worksheet in the workbook.
Worksheet wsTemplate = excelWorkbook.getWorksheets().get(0);
// Copy the first column from the first worksheet of the first workbook into the first worksheet of the second workbook.
wsTemplate.getCells().copyColumn(wsTemplate.getCells(), 1, 4);
// Save the excel file.
excelWorkbook.save(dataDir + "CopyingColumns_out.xls");
// Print message
System.out.println("Row and Column copied successfully.");

复制多列

相近[Cells.copyRows](https://reference.aspose.com/cells/java/com.aspose.cells/cells#copyRow(com.aspose.cells.Cells,%20int,%20int) 方法,Aspose.Cells API 还提供[Cells.copyColumns](https://reference.aspose.com/cells/java/com.aspose.cells/cells#copyColumns(com.aspose.cells.Cells,%20int,%20int,%20int)方法以便将多个源列复制到新位置。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(CopyingMultipleColumns.class);
// Create an instance of Workbook class by loading the existing spreadsheet
Workbook workbook = new Workbook(dataDir + "aspose-sample.xlsx");
// Get the cells collection of worksheet by name Columns
Cells cells = workbook.getWorksheets().get("Columns").getCells();
// Copy the first 3 columns 7th column
cells.copyColumns(cells, 0, 6, 3);
// Save the result on disc
workbook.save(dataDir + "output.xlsx");

以下是源电子表格和生成的电子表格在 Excel 中的外观。

待办事项:图片_替代_文本

待办事项:图片_替代_文本

使用粘贴选项粘贴行/列

Aspose.Cells现在提供粘贴选项在使用功能时[复制行](https://reference.aspose.com/cells/java/com.aspose.cells/cells#copyRows(com.aspose.cells.Cells,%20int,%20int,%20int,%20com.aspose.cells.CopyOptions,%20com.aspose.cells.PasteOptions) ) 和复制列.它允许设置类似于 Excel 的适当粘贴选项。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Load some excel file
Workbook wb = new Workbook("book1.xlsx");
// Access the first sheet which contains chart
Worksheet source = wb.getWorksheets().get(0);
// Add another sheet named DestSheet
Worksheet destination = wb.getWorksheets().add("DestSheet");
// Set CopyOptions.ReferToDestinationSheet to true
CopyOptions options = new CopyOptions();
options.setReferToDestinationSheet(true);
// Set PasteOptions
PasteOptions pasteOptions = new PasteOptions();
pasteOptions.setPasteType(PasteType.VALUES);
pasteOptions.setOnlyVisibleCells(true);
// Copy all the rows of source worksheet to destination worksheet which includes chart as well
// The chart data source will now refer to DestSheet
destination.getCells().copyRows(source.getCells(), 0, 0, source.getCells().getMaxDisplayRange().getRowCount(), options, pasteOptions);
// Save workbook in xlsx format
wb.save("destination.xlsx", SaveFormat.XLSX);