复制行和列

介绍

有时,您需要在不复制整个工作表的情况下复制工作表中的行和列。使用Aspose.Cells,可以在工作簿内部或工作簿之间复制行和列。 复制行(或列)时,其中包含的数据,包括更新的引用的公式和值,注释,格式,隐藏单元格,图像以及其他绘图对象也会被复制。

使用Microsoft Excel如何复制行和列

  1. 选择要复制的行或列。
  2. 要复制行或列,请单击 标准 工具栏上的 复制,或按 CTRL+C
  3. 选择要复制所选内容下方或右侧的行或列。
  4. 复制行或列时,单击 已复制的单元格插入 菜单上。

如何使用Microsoft Excel的粘贴选项粘贴行和列

  1. 选择包含您要复制的数据或其他属性的单元格。
  2. 在主页选项卡上,单击复制
  3. 单击要在其中粘贴所复制内容的区域中的第一个单元格。
  4. 在主页选项卡上,单击粘贴旁边的箭头,然后选择粘贴特殊。
  5. 选择您想要的选项

如何使用 Aspose.Cells for .NET 复制行和列

如何复制单行

Aspose.Cells提供copy_row类的Cells方法。此方法将所有类型的数据复制到目标行,包括公式,值,注释,单元格格式,隐藏单元格,图像和其他绘图对象。

copy_row 方法接受以下参数:

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

使用此方法在工作表内或另一工作表之间复制一行。 copy_row 方法的工作原理类似于 Microsoft Excel。例如,不需要明确设置目标行高度,该值也会被复制。

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

您可以跳过使用 Cells.get_row_height 方法获取源行高度然后使用 Cells.set_row_height 方法设置目标行高度的步骤,因为 copy_row 方法会自动处理行高度。

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Open the existing excel file.
excelWorkbook1 = Workbook(dataDir + "book1.xls")
# Get the first worksheet in the workbook.
wsTemplate = excelWorkbook1.worksheets[0]
# Copy the second row with data, formattings, images and drawing objects
# To the 16th row in the worksheet.
wsTemplate.cells.copy_row(wsTemplate.cells, 1, 15)
# Save the excel file.
excelWorkbook1.save(dataDir + "output.xls")

如何复制多行

您还可以在使用 Cells.copy_rows 方法将多行复制到新目标时,该方法需要一个额外的整数参数,用于指定要复制的源行数。

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create an instance of Workbook class by loading the existing spreadsheet
workbook = Workbook(dataDir + "aspose-sample.xlsx")
# Get the cells collection of worksheet by name Rows
cells = workbook.worksheets.get("Rows").cells
# Copy the first 3 rows to 7th row
cells.copy_rows(cells, 0, 6, 3)
# Save the result on disc
workbook.save(dataDir + "output_out.xlsx")

如何复制列

Aspose.Cells 提供 Cells 类的 copy_column 方法,此方法复制来自源列到目标列的各种数据,包括更新后的引用公式和数值、注释、单元格格式、隐藏单元格、图像和其他绘图对象。

copy_column 方法接受以下参数:

  • 来源 Cells 对象,
  • 源列索引, 和
  • 目标列索引.

使用 copy_column 方法在工作表内或另一工作表中复制列。

该示例将一个工作表中的列复制到另一个工作簿的工作表中。

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create another Workbook.
excelWorkbook1 = Workbook(dataDir + "book1.xls")
# Get the first worksheet in the book.
ws1 = excelWorkbook1.worksheets[0]
# Copy the first column from the first worksheet of the first workbook into
# The first worksheet of the second workbook.
ws1.cells.copy_column(ws1.cells, ws1.cells.columns[0].index, ws1.cells.columns[2].index)
# Autofit the column.
ws1.auto_fit_column(2)
# Save the excel file.
excelWorkbook1.save(dataDir + "output.xls")

如何复制多列

Cells.copy_rows 方法类似,Aspose.Cells API 还提供 Cells.copy_columns 方法,用于将多个源列复制到新位置。

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create an instance of Workbook class by loading the existing spreadsheet
workbook = Workbook(dataDir + "aspose-sample.xlsx")
# Get the cells collection of worksheet by name Columns
cells = workbook.worksheets.get("Columns").cells
# Copy the first 3 columns 7th column
cells.copy_columns(cells, 0, 6, 3)
# Save the result on disc
workbook.save(dataDir + "output_out.xlsx")

如何粘贴行和列并设置粘贴选项

Aspose.Cells 现在提供 PasteOptions,使用函数 copy_rowscopy_columns。它允许设置类似于 Excel 的适当粘贴选项。

from aspose.cells import CopyOptions, PasteOptions, PasteType, SaveFormat, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
# Output directory
outputDir = RunExamples.Get_OutputDirectory()
# Load sample excel file
wb = Workbook(sourceDir + "sampleChangeChartDataSource.xlsx")
# Access the first sheet which contains chart
source = wb.worksheets[0]
# Add another sheet named DestSheet
destination = wb.worksheets.add("DestSheet")
# Set CopyOptions.ReferToDestinationSheet to true
options = CopyOptions()
options.refer_to_destination_sheet = True
# Set PasteOptions
pasteOptions = PasteOptions()
pasteOptions.paste_type = PasteType.VALUES
pasteOptions.only_visible_cells = 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.cells.copy_rows(source.cells, 0, 0, source.cells.max_display_range.row_count, options, pasteOptions)
# Save workbook in xlsx format
wb.save(outputDir + "outputChangeChartDataSource.xlsx", SaveFormat.XLSX)