Xceed Workbooks for .NET v3.0 Documentation
Xceed.Workbooks.NET Assembly / Xceed.Workbooks.NET Namespace / WorksheetCollection Class / Copy Method
Example


In This Topic
    Copy Method
    In This Topic
    This method is used to create a copy of a specific worksheet. The worksheet to be copied can be specified by its index, name or by using the Worksheet object itself.
    Overload List
    Remarks

    The optional SheetName parameter will rename the copied Worksheet with the desired name if the name is available. If this parameter is left blank, the name given to the Worksheet will be the name of the original Worksheet with a number. For example, a Worksheet named Sheet could be called [Sheet (2)].

    The destination Worksheet Index will move the copied Worksheet to the designated index in the collection. If the index is null, it will go to the end of the collection.

    Example
    using( var workbook = Workbook.Create( "test.xlsx" ) )
      {
        // Get the first worksheet. A workbook contains at least 1 worksheet.
        var worksheet = workbook.Worksheets[ 0 ];
    
        // Set values in cells.
        worksheet.Rows[ 4 ].Cells[ 5 ].Value = "data.";
    
        // Copy first worksheet and name it "The new name".
        // The worksheet to copy can be identified by an id, a name or an instance.
        var worksheetCopy = workbook.Worksheets.Copy( 0, "The new name" );
           
        // Input new values in copied worksheet.
        worksheetCopy.Cells[ "A10" ].Value = "data2";
    
        // Save workbook to disk.
        workbook.Save();
      }
    Requirements

    Target Platforms: Windows 11, Windows 10, Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

    See Also