GcExcel .NET provides users with the ability to cut or copy a cell or a range of cells from a specific area and paste it into another area within the same worksheet. Also, users can choose from multiple paste options and also combine different paste options while copying the data from the cell range.
You can refer to the following sections in order to cut or copy data from the cell range.
In order to cut or copy data across multiple sheets, refer to Cut or Copy Across Sheets.
GcExcel allows you to copy a cell or a range of cells in the worksheets by calling Copy method of IRange. To copy a single cell or a range of cells, specify the cell range to be copied, for example B3:D12.
GcExcel provides the following different ways to use the Copy method.
Example | Description |
---|---|
Copy(sheet.Range["E5"]) | This method copies data from cell range B3:D12 and pastes the data to cell E5 onwards. |
Copy(sheet.Range["E5:G14"]) | This method copies data from cell range B3:D12 and pastes the data in cell range E5:G14. In case the range of cells copied does not fit into the destination cell range, the data is lost. |
Refer to the following example code in order to copy the cell range in a workbook.
C# |
Copy Code |
---|---|
// Copy the data of the range of cells worksheet.Range["B3:D12"].Copy(worksheet.Range["E5"]); //Or worksheet.Range["B3:D12"].Copy(worksheet.Range["E5:G14"]); |
Users can choose from several paste options while copying the data from the cell range. The PasteType enumeration can be used to work with multiple paste options as described in the table shared below.
Option | Description |
---|---|
Default | This option can be used to paste all the cell data to the destination range except the row heights and column widths. |
Values | This option can be used to paste only the cell value to the destination. |
Formulas | If you're working in a formula cell, this option can be used to paste the formula to the destination . However, for a non-formula cell, this option pastes the cell value to the destination. |
Formats | This option can be used to paste formats. |
NumberFormats | This option can be used to paste number formats. |
RowHeights | This option can be used to paste the row height to the destination. |
ColumnWidths | This option can be used to paste the column width to the destination. |
Users can also combine the two different paste options. For instance - if users want to paste values and number formats concurrently in the worksheet, then they can use combinations like : PasteType.Values | PasteType.NumberFormats , PasteType.Formulas | PasteType.NumberFormats. Similarly other paste options can also be combined with each other.
Refer to the following example code in order to use the combination of paste options while copying data from the cell range in a workbook and paste it to the destination.
C# |
Copy Code |
---|---|
// Initialize workbook Workbook workbook = new Workbook(); // Fetch default worksheet IWorksheet worksheet = workbook.Worksheets[0]; // Set data of PC worksheet.Range["A2"].Value = "PC"; worksheet.Range["A4:C4"].Value = new string[] { "Device", "Quantity", "Unit Price" }; worksheet.Range["A5:C10"].Value = new object[,] { { "T540p", 12, 9850 }, { "T570", 5, 7460 }, { "Y460", 6, 5400 }, { "Y460F", 8, 6240 } }; // Set style worksheet.Range["A2"].RowHeight = 30; worksheet.Range["A2"].ColumnWidth = 40; worksheet.Range["A2"].Font.Size = 20; worksheet.Range["A2"].Font.Bold = true; worksheet.Range["A4:C4"].Font.Bold = true; worksheet.Range["A4:C4"].Font.Color = Color.White; worksheet.Range["A4:C4"].Interior.Color = Color.LightBlue; worksheet.Range["A5:C10"].Borders[BordersIndex.InsideHorizontal].Color = Color.Orange; worksheet.Range["A5:C10"].Borders[BordersIndex.InsideHorizontal].LineStyle = BorderLineStyle.DashDot; // Copy only style and row height from cells A2:C10 worksheet.Range["H1"].Value = "Copy style & row height from previous cells."; worksheet.Range["H1"].Font.Color = Color.Red; worksheet.Range["H1"].Font.Bold = true; worksheet.Range["A2:C10"].Copy(worksheet.Range["H2"], PasteType.Formats | PasteType.ColumnWidths); // Set data of mobile devices worksheet.Range["H2"].Value = "Mobile"; worksheet.Range["H4:J4"].Value = new string[] { "Device", "Quantity", "Unit Price" }; worksheet.Range["H5:J10"].Value = new object[,] { { "HW-P30", 20, 4200 }, { "IPhone-X", 5, 9888 }, { "IPhone-6s plus", 15, 6880 } }; // Add new sheet IWorksheet worksheet2 = workbook.Worksheets.Add(); // Copy only style of Cell A2:C10 to new sheet worksheet.Range["A2:C10"].Copy(worksheet2.Range["A2"], PasteType.Formats | PasteType.ColumnWidths); worksheet2.Range["A3"].Value = "Copy style from sheet1."; worksheet2.Range["A3"].Font.Color = Color.Red; worksheet2.Range["A3"].Font.Bold = true; // Saving workbook to xlsx workbook.Save(@"PasteOptionsEnhancements.xlsx", SaveFileFormat.Xlsx); |
GcExcel allows you to cut a cell or a range of cells in the worksheet by calling the Cut method of the IRange interface. To cut a cell or the range of cells, specify the cell range to be moved, for example B3:D12.
GcExcel provide the following different ways to use Cut method.
Example | Description |
---|---|
Cut(sheet.Range["E5"]) | This method cuts the data from cell range B3:D12 and pastes the data to cell E5 onwards. |
Cut(sheet.Range["E5:G14"]) | This method cuts the data from cell range B3:D12 and pastes the data in cell range E5:G14. In case the range of cells cut does not fit into the destination cell range, the data is lost. |
Refer to the following example code to cut a range of cells in the workbook.
C# |
Copy Code |
---|---|
// Cut the data of the range of cell worksheet.Range["B3:D12"].Cut(worksheet.Range["E5"]); // Or worksheet.Range["B3:D12"].Cut(worksheet.Range["E5:G14"]); |