复制行和列
介绍
有时,您需要在不复制整个工作表的情况下复制工作表中的行和列。使用Aspose.Cells,可以在工作簿内部或工作簿之间复制行和列。 复制行(或列)时,其中包含的数据,包括更新的引用的公式和值,注释,格式,隐藏单元格,图像以及其他绘图对象也会被复制。
使用Microsoft Excel如何复制行和列
- 选择要复制的行或列。
- 要复制行或列,请单击 标准 工具栏上的 复制,或按 CTRL+C。
- 选择要复制所选内容下方或右侧的行或列。
- 复制行或列时,单击 已复制的单元格 在 插入 菜单上。
如何使用Microsoft Excel的粘贴选项粘贴行和列
- 选择包含您要复制的数据或其他属性的单元格。
- 在主页选项卡上,单击复制。
- 单击要在其中粘贴所复制内容的区域中的第一个单元格。
- 在主页选项卡上,单击粘贴旁边的箭头,然后选择粘贴特殊。
- 选择您想要的选项。
如何使用 Aspose.Cells for .NET 复制行和列
如何复制单行
Aspose.Cells提供CopyRow类的Cells方法。此方法将所有类型的数据复制到目标行,包括公式,值,注释,单元格格式,隐藏单元格,图像和其他绘图对象。
CopyRow 方法接受以下参数:
- 来源 Cells 对象,
- 源行索引, 和
- 目标行索引.
使用此方法在工作表内或另一工作表之间复制一行。 CopyRow 方法的工作原理类似于 Microsoft Excel。例如,不需要明确设置目标行高度,该值也会被复制。
以下示例显示如何在工作表中复制一行。它使用一个模板 Microsoft Excel 文件,将第二行(包括数据、格式、注释、图像等)复制并粘贴到同一工作表的第12行。
您可以跳过使用 Cells.GetRowHeight 方法获取源行高度然后使用 Cells.SetRowHeight 方法设置目标行高度的步骤,因为 CopyRow 方法会自动处理行高度。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Open the existing excel file. | |
Workbook excelWorkbook1 = new Workbook(dataDir + "book1.xls"); | |
// Get the first worksheet in the workbook. | |
Worksheet wsTemplate = excelWorkbook1.Worksheets[0]; | |
// Copy the second row with data, formattings, images and drawing objects | |
// To the 16th row in the worksheet. | |
wsTemplate.Cells.CopyRow(wsTemplate.Cells, 1, 15); | |
// Save the excel file. | |
excelWorkbook1.Save(dataDir + "output.xls"); |
在复制行时,重要的是注意相关的图像、图表或其他绘图对象,因为这与 Microsoft Excel 相同:
- 如果源行索引为5,则如果它包含在三行内(起始行索引为4,结束行索引为6),则图像、图表等会被复制。
- 目标行中现有的图像、图表等不会被移除。
如何复制多行
您还可以在使用 Cells.CopyRows 方法将多行复制到新目标时,该方法需要一个额外的整数参数,用于指定要复制的源行数。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// 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.Worksheets["Rows"].Cells; | |
// Copy the first 3 rows to 7th row | |
cells.CopyRows(cells, 0, 6, 3); | |
// Save the result on disc | |
workbook.Save(dataDir + "output_out.xlsx"); |
如何复制列
Aspose.Cells 提供 Cells 类的 CopyColumn 方法,此方法复制来自源列到目标列的各种数据,包括更新后的引用公式和数值、注释、单元格格式、隐藏单元格、图像和其他绘图对象。
CopyColumn 方法接受以下参数:
- 来源 Cells 对象,
- 源列索引, 和
- 目标列索引.
使用 CopyColumn 方法在工作表内或另一工作表中复制列。
该示例将一个工作表中的列复制到另一个工作簿的工作表中。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create another Workbook. | |
Workbook excelWorkbook1 = new Workbook(dataDir + "book1.xls"); | |
// Get the first worksheet in the book. | |
Worksheet 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.CopyColumn(ws1.Cells, ws1.Cells.Columns[0].Index, ws1.Cells.Columns[2].Index); | |
// Autofit the column. | |
ws1.AutoFitColumn(2); | |
// Save the excel file. | |
excelWorkbook1.Save(dataDir + "output.xls"); |
如何复制多列
与 Cells.CopyRows 方法类似,Aspose.Cells API 还提供 Cells.CopyColumns 方法,用于将多个源列复制到新位置。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// 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.Worksheets["Columns"].Cells; | |
// Copy the first 3 columns 7th column | |
cells.CopyColumns(cells, 0, 6, 3); | |
// Save the result on disc | |
workbook.Save(dataDir + "output_out.xlsx"); |
如何粘贴行和列并设置粘贴选项
Aspose.Cells 现在提供 PasteOptions,使用函数 CopyRows 和 CopyColumns。它允许设置类似于 Excel 的适当粘贴选项。
// 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 | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
//Output directory | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
// Load sample excel file | |
Workbook wb = new Workbook(sourceDir + "sampleChangeChartDataSource.xlsx"); | |
// Access the first sheet which contains chart | |
Worksheet source = wb.Worksheets[0]; | |
// Add another sheet named DestSheet | |
Worksheet destination = wb.Worksheets.Add("DestSheet"); | |
// Set CopyOptions.ReferToDestinationSheet to true | |
CopyOptions options = new CopyOptions(); | |
options.ReferToDestinationSheet = true; | |
// Set PasteOptions | |
PasteOptions pasteOptions = new PasteOptions(); | |
pasteOptions.PasteType = PasteType.Values; | |
pasteOptions.OnlyVisibleCells = 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.CopyRows(source.Cells, 0, 0, source.Cells.MaxDisplayRange.RowCount, options, pasteOptions); | |
// Save workbook in xlsx format | |
wb.Save(outputDir + "outputChangeChartDataSource.xlsx", SaveFormat.Xlsx); |