Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

Description

Excerpt

Copies an area of cells from a Worksheet in either the same workbook or another workbook.
You may specify the types of data copied (cell value, formulas, comments, formatting, etc.) using the CopyPasteProperties object.
The CopyPaste method supports copying column width, comments, formulas, merged cells, row height, values, and cell and number formatting.The CopyPaste method does not currently support copying anything not listed above, including charts, images, or autofilters.

Signature
C#
C#
 public Area CopyPaste(int firstRow, int firstCol, Area sourceArea, CopyPasteProperties properties)
{signature}
{signature:
}
Signature
vb.net
vb.net
Public Function CopyPaste(ByVal firstRow As Integer, ByVal firstCol As Integer, ByVal sourceArea As Area, ByVal properties As CopyPasteProperties) As Area
{signature}
{parameters}
{param:firstRow}The zero\-based index of the first row in destination area.{param}
{param:firstCol}The zero\-based index of the first column in the destination area.{param}
{param:sourceArea}An area from the source workbook from which to copy.{param}
{param:properties}A [CopyPasteProperties|CopyPasteProperties] object that specifies the types of data to copy.{param}
{returns}An [Area|Area] object representing the set of cells populated with the copied values.{returns}
{remarks}
* The copied data will +overwrite+ the contents of the cells in this worksheet.
* The CopyPaste method does not copy conditional formatting.
* When formulas are copied, references to ranges are updated to refer to the new sheet.

{remarks}
{example}{code:csharp|title=C#}
Parameters
Param
firstRow
firstRow

The zero-based index of the first row in destination area.

Param
firstCol
firstCol

The zero-based index of the first column in the destination area.

Param
sourceArea
sourceArea

An area from the source workbook from which to copy.

Param
properties
properties

A CopyPasteProperties object that specifies the types of data to copy.

Returns

An Area object representing the set of cells populated with the copied values.

Remarks
  • The copied data will overwrite the contents of the cells in this worksheet.
  • The CopyPaste method does not copy conditional formatting.
  • When formulas are copied, references to ranges are updated to refer to the new sheet.
Example
Code Block
csharp
csharp
titleC#

//--- Open the workbook and worksheet from which we will copy cells.
ExcelApplication xla = new ExcelApplication();
Workbook sourceWb = xla.Open(Page.MapPath("myfile.xlsx"));
Worksheet sourceWs = sourceWb[0];
//--- Create the destination workbook and get the worksheet
//--- into which we will paste cells.
Workbook destWb = xla.Create(ExcelApplication.FileFormat.Xlsx);
Worksheet destWs = destWb[0];
//--- Define the area of cells to copy.
Area srcArea = sourceWs.CreateArea("A1:F20");
//--- Create the CopyPasteProperties object and define which settings will be copied.
CopyPasteProperties copyPasteProps =
sourceWb.CreateCopyPasteProperties(CopyPasteProperties.CopyPasteType.ValuesAndFormatting);
//--- Paste the data onto the destination worksheet.
   destWs.CopyPaste(0, 0, srcArea, copyPasteProps);


{code} {code:
Code Block
vb.net
|title=
vb.net
titlevb.net
}


 '--- Open the workbook and worksheet from which we will copy cells.
 Dim sourceWb As Workbook = xla.Open("MyDataSource.xls")
 Dim sourceWs As Worksheet = sourceWb(0)

 '--- Create the workbook and and get the worksheet
 '--- into which we will paste cells.
 Dim destWb As Workbook = xla.Create("MyNewWorkbook")
 Dim destWs As Worksheet = destWb(0)

 '--- Define the area of cells to copy.
 Dim srcArea As Area = sourceWs.CreateArea("A1:F20")

 '--- Copy cell values, formulas and cell and number formatting.
 Dim copyPasteProps As CopyPasteProperties = _
 sourceWb.CreateCopyPasteProperties( _
 CopyPasteProperties.CopyPasteType.ValueFormulasAndFormatting)

 '--- Paste the data onto the destination worksheet.
 destWs.CopyPaste(0, 0, srcArea, copyPasteProps)
 
{code} {example}