Spread Windows Forms 11.0 Product Documentation
Copying and Inserting a Sheet
Spread Windows Forms 11.0 Product Documentation > Developer's Guide > Understanding the Spreadsheet Objects > Working with Sheets > Copying and Inserting a Sheet

You can copy and insert a sheet to the same Spread component or another Spread component on the form. Spread does not provide a way to copy the sheet, but with the code given below you can easily create your own CopySheet method. To copy a sheet and insert it in the component, simply create a new method, called CopySheet, as shown here and then use the Add or Insert methods in the SheetViewCollection class.

The CopySheet method also copies all shapes on that sheet.

Copying a sheet using the CopySheet method also copies the NamedStyleCollection in the sheet, and creates separate copies of any NamedStyle objects in the collection that are private to the copy and not shared with the original NamedStyleCollection in the copied sheet. If you want to share the named styles instead of creating separate copies, you can assign the NamedStyleCollection you want to share to the NamedStyles property of the copy. You might also want to temporarily remove the NamedStyleCollection from the sheet being copied, so that it is not copied unnecessarily. This can be done by assigning the NamedStyleCollection to a variable, then setting the NamedStyles property to Nothing (null in C#), then making the copy, then assigning the variable back to the NamedStyles property.

The Spread Designer can be used to copy and paste a sheet at design time. Right-click on the sheet tab icon in the designer to bring up the Copy, Cut, and Paste context menu.

The SpreadActions class has options for the clipboard copy, cut, and paste of a sheet.

Using Code

  1. Create a new CopySheet method to be used for copying sheets.
  2. Handle the named styles for that sheet as described above.
  3. Call the Sheets shortcut object Add method to add the new sheet or the Insert method to insert the sheet to the SheetViewCollection for the component.

Example

This is the code for the CopySheet method.

C#
Copy Code
private void Form1_Load(object sender, EventArgs e)
{
  fpSpread1.Sheets.Count = 3;
}

private void button1_Click(object sender, EventArgs e)
{
  FarPoint.Win.Spread.SheetView s = new FarPoint.Win.Spread.SheetView();
  s.Cells[0, 0].Text = "test";
  FarPoint.Win.Spread.DrawingSpace.FourWayArrowShape sh = new FarPoint.Win.Spread.DrawingSpace.FourWayArrowShape();
  sh.Name = "Arrow";
  s.AddShape(sh);
  fpSpread1.Sheets.Add(CopySheet(s));
}

public FarPoint.Win.Spread.SheetView CopySheet(FarPoint.Win.Spread.SheetView sheet)
        {
            FarPoint.Win.Spread.SheetView newSheet = null;
            if (sheet != null)
            {
                newSheet = (FarPoint.Win.Spread.SheetView)FarPoint.Win.Serializer.LoadObjectXml(typeof(FarPoint.Win.Spread.SheetView), FarPoint.Win.Serializer.GetObjectXml(sheet, "CopySheet"), "CopySheet");
            }
            return newSheet;
        }
VB
Copy Code
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  fpSpread1.Sheets.Count = 3
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  Dim s As New FarPoint.Win.Spread.SheetView()
  s.Cells(0, 0).Text = "test"
  Dim sh As New FarPoint.Win.Spread.DrawingSpace.FourWayArrowShape()
  sh.Name = "Arrow"
  s.AddShape(sh)
  fpSpread1.Sheets.Add(CopySheet(s))
End Sub

Public Function CopySheet(sheet As FarPoint.Win.Spread.SheetView) As FarPoint.Win.Spread.SheetView
   Dim newSheet as FarPoint.Win.Spread.SheetView = Nothing
   If Not IsNothing(sheet) Then
     newSheet = FarPoint.Win.Serializer.LoadObjectXml(GetType(FarPoint.Win.Spread.SheetView), FarPoint.Win.Serializer.GetObjectXml(sheet, "CopySheet"), "CopySheet")
   End If
   Return newSheet
End Function
See Also