复制行和列
介绍
有时,您需要在不复制整个工作表的情况下复制工作表中的行和列。使用Aspose.Cells,可以在工作簿内部或工作簿之间复制行和列。
复制行(或列)时,其中包含的数据,包括更新的引用的公式和值,注释,格式,隐藏单元格,图像以及其他绘图对象也会被复制。
使用Microsoft Excel复制行和列
- 选择要复制的行或列。
- 要复制行或列,请单击 标准 工具栏上的 复制,或按 CTRL+C。
- 选择要复制所选内容下方或右侧的行或列。
- 复制行或列时,单击 已复制的单元格 在 插入 菜单上。
复制单行
Aspose.Cells提供了copyRow 方法,用于Cells 类。 该方法将源行中的所有类型数据(包括公式、值、注释、单元格格式、隐藏单元格、图像和其他绘图对象)复制到目标行。
copyRow 方法需要以下参数:
- 源Cells对象,
- 源行索引, 和
- 目标行索引.
使用此方法在工作表内或者工作表之间复制行。 copyRow 方法的工作方式与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."); |
执行下面代码后生成以下输出。
行被以最高程度的精度和准确性复制
在复制行时,重要的是注意相关的图像、图表或其他绘图对象,因为这与 Microsoft Excel 相同:
- 如果源行索引为5,则如果它包含在三行内(起始行索引为4,结束行索引为6),则图像、图表等会被复制。
- 目标行中现有的图像、图表等不会被移除。
复制多行
在使用Cells.copyRows方法时,您还可以将多行复制到新的目的地,并附加一个整数类型的额外参数来指定要复制的源行数。
以下是包含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提供了copyColumn方法,该方法可将源列中的所有类型数据(包括更新引用的公式和值,注释,单元格格式,隐藏单元格,图像和其他绘图对象)复制到目标列。
copyColumn方法接受以下参数:
- 源Cells对象,
- 源列索引, 和
- 目标列索引.
使用copyColumn方法在工作表内部或其他工作表之间复制列。
该示例将一个工作表中的列复制到另一个工作簿的工作表中。
从一个工作簿复制列到另一个工作簿
// 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 方法类似,Aspose.Cells API 还提供 Cells.copyColumns 方法,用于将多个源列复制到新位置。
// 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现在在使用CopyRows和CopyColumns函数时提供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); |