You can copy and insert a sheet to the same Spread component or another Spread component. Spread allows you to create copies of sheets using the Copy method of the IWorksheets interface.
Alternatively, you can also create your own custom CopySheet method to copy a sheet. In this scenario, to insert a copied sheet into the control, use the Add or Insert method of the SheetViewCollection class referenced by the Sheets property of the FpSpread class after calling the CopySheet method.
Here is the example code for the CopySheet method.
C# |
Copy Code
|
---|---|
public FarPoint.Win.Spread.SheetView CopySheet(FarPoint.Win.Spread.SheetView sheet) { FarPoint.Win.Spread.SheetView newSheet = null; if (sheet != null ) { newSheet = FarPoint.Win.Serializer.LoadObjectXml(GetType(FarPoint.Win.Spread.SheetView), FarPoint.Win.Serializer.GetObjectXml(sheet, "CopySheet"), "CopySheet"); } return newSheet; } |
Visual Basic |
Copy Code
|
---|---|
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 |
Using Spread Designer
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.
Spread allows you to create copies of sheets having cell formulas. To do this, you can use the Copy method of the IWorksheets interface. This method allows you to copy both the cell’s destination and reference sheets at the same time, which then creates a reference state within the multiple copied sheets.
Using code
The following example code shows how to copy sheets with formula references.
C# |
Copy Code
|
---|---|
var sheet2 = fpSpread1.AsWorkbook().Worksheets.Add(); sheet2.ColumnHeader.RowCount = 2; sheet2.ColumnHeader.Cells["A1"].Value = 5; sheet2.Cells["A1"].Value = 4; IWorksheet TestActiveSheet = fpSpread1.AsWorkbook().ActiveSheet; TestActiveSheet.Cells[0, 0].Formula = "Sum(Sheet2!A1,A2)"; TestActiveSheet.Cells[0, 1].Formula = "Sheet1[#Headers,[A1]]"; fpSpread1.AsWorkbook().Worksheets[0, 1].Copy(0); |
Visual Basic |
Copy Code
|
---|---|
Dim sheet2 = FpSpread1.AsWorkbook().Worksheets.Add() sheet2.ColumnHeader.RowCount = 2 sheet2.ColumnHeader.Cells("A1").Value = 5 sheet2.Cells("A1").Value = 4 Dim TestActiveSheet As IWorksheet = FpSpread1.AsWorkbook().ActiveSheet TestActiveSheet.Cells(0, 0).Formula = "Sum(Sheet2!A1,A2)" TestActiveSheet.Cells(0, 1).Formula = "Sheet1[#Headers,[A1]]" FpSpread1.AsWorkbook().Worksheets(0, 1).Copy(0) |
At Runtime
Follow the below steps to copy the sheets having cell formulas and referenced sheet:
C# |
Copy Code
|
---|---|
fpSpread1.Width = 800; fpSpread1.TabStripRatio = 0.8f; fpSpread1.TabStrip.Editable = true; var sheet2 = fpSpread1.AsWorkbook().Worksheets.Add(); sheet2.ColumnHeader.RowCount = 2; sheet2.ColumnHeader.Cells["A1"].Value = 5; sheet2.Cells["A1"].Value = 4; IWorksheet TestActiveSheet = fpSpread1.AsWorkbook().ActiveSheet; TestActiveSheet.Cells[0, 0].Formula = "Sum(Sheet2!A1,A2)"; TestActiveSheet.Cells[0, 1].Formula = "Sheet2[#Headers,[A1]]"; |
Visual Basic |
Copy Code
|
---|---|
FpSpread1.Width = 800 FpSpread1.TabStripRatio = 0.8F FpSpread1.TabStrip.Editable = True Dim sheet2 = FpSpread1.AsWorkbook().Worksheets.Add() sheet2.ColumnHeader.RowCount = 2 sheet2.ColumnHeader.Cells("A1").Value = 5 sheet2.Cells("A1").Value = 4 Dim TestActiveSheet As IWorksheet = FpSpread1.AsWorkbook().ActiveSheet TestActiveSheet.Cells(0, 0).Formula = "Sum(Sheet2!A1,A2)" TestActiveSheet.Cells(0, 1).Formula = "Sheet2[#Headers,[A1]]" |