Copying Rows and Columns
Introduction
Sometimes, you need to copy rows and columns in a worksheet without copying the entire worksheet. With Aspose.Cells, it is possible to copy rows and columns within or between workbooks. When a row (or column) is copied, the data contained in it, including formulas - with updated references - and values, comments, formatting, hidden cells, images, and other drawing objects are copied too.
How to Copy Rows and Columns with Microsoft Excel
- Select the row or column that you want to copy.
- To copy rows or columns, click Copy on the Standard toolbar, or press CTRL+C.
- Select a row or column below or to the right of where you want to copy your selection.
- When you are copying rows or columns, click Copied Cells on the Insert menu.
How to Paste Rows and Columns using Paste Options with Microsoft Excel
- Select the cells that contain the data or other attributes that you want to copy.
- On the Home tab, click Copy.
- Click the first cell in the area where you want to paste what you copied.
- On the Home tab, click the arrow next to Paste, and then select Paste Special.
- Select the options you want.
How to Copy Rows and Columns Using Aspose.Cells for .NET
How to Copy Single Rows
Aspose.Cells provides the CopyRow method of the Cells class. This method copies all types of data including formulas, values, comments, cell formats, hidden cells, images and other drawing objects from the source row to the destination row.
The CopyRow method takes the following parameters:
- the source Cells object,
- the source row index, and
- the destination row index.
Use this method to copy a row within a sheet, or to another sheet. The CopyRow method works in a similar way to Microsoft Excel. So, for example, you don’t need to set the height of the destination row explicitly, that value is copied too.
The following example shows how to copy a row in a worksheet. It uses a template Microsoft Excel file and copies the second row (complete with data, formatting, comments, images and so on) and paste it to the 12th row in the same worksheet.
You can skip the step that gets the source row height using the Cells.GetRowHeight method and then sets the destination row height using the Cells.SetRowHeight method as the CopyRow method automatically takes care of the row height.
// 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"); |
When copying rows, it is important to note related images, charts or other drawing objects as this is the same with Microsoft Excel:
- If the source row index is 5, the image, chart, etc., is copied if it is contained in the three rows (the starting row index is 4 and the ending row index is 6).
- The existing images, charts, etc. in the destination row will not be removed.
How to Copy Multiple Rows
You can also copy multiple rows onto a new destination while using the Cells.CopyRows method which takes an additional parameter of type integer to specify the number of source rows to be copied.
// 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"); |
How to Copy Columns
Aspose.Cells provides the CopyColumn method of the Cells class, this method copies all types of data, including formulas - with updated references - and values, comments, cell formats, hidden cells, images and other drawing objects from the source column to the destination column.
The CopyColumn method takes the following parameters:
- the source Cells object,
- source column index, and
- the destination column index.
Use the CopyColumn method to copy a column within a sheet or to another sheet.
This example copies a column from a worksheet and pastes it into a worksheet in another 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. | |
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"); |
How to Copy Multiple Columns
Similar to Cells.CopyRows method, the Aspose.Cells APIs also provide the Cells.CopyColumns method in order to copy multiple source columns to a new location.
// 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"); |
How to Paste Rows and Columns with Paste Options
Aspose.Cells now provides PasteOptions while using functions CopyRows and CopyColumns. It allows to set appropriate paste option similar to 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); |