-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMoveAndCopySample.code.cs
44 lines (36 loc) · 1.9 KB
/
MoveAndCopySample.code.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// NOTE: a version of this sample is available to run on our website at:
// https://www.spreadsheetgear.com/Support/Samples/API/WorksheetMoveAndCopy
namespace SamplesLibrary.Engine.Samples.Workbook.Worksheet
{
public class MoveAndCopySample : ISpreadsheetGearEngineSample
{
public SpreadsheetGear.IWorkbook Workbook { get; set; }
public void InitializeWorkbook()
{
// Create a new workbook.
Workbook = SpreadsheetGear.Factory.GetWorkbook();
}
public void RunSample()
{
// Get the workbook set that 'Workbook' belongs to.
SpreadsheetGear.IWorkbookSet workbookSet = Workbook.WorkbookSet;
// Get the full path to a workbook template file.
string workbookPath = Helpers.GetFullOutputFolderPath(@"Files\Engine\MoveAndCopyTemplate.xlsx");
// Open the workbook in the Workbook Set.
SpreadsheetGear.IWorkbook template = workbookSet.Workbooks.Open(workbookPath);
// Make a copy of template sheet in new workbook, which will be placed after Sheet1.
SpreadsheetGear.IWorksheet newSheet = template.Worksheets["Template Sheet"].CopyAfter(
Workbook.Worksheets["Sheet1"]) as SpreadsheetGear.IWorksheet;
// Rename the sheet
newSheet.Name = "Profit Report";
// Populate new sheet with some dummy data.
string formula = "=ROUND(RAND()*1000000,2)";
newSheet.Names["NorthProfit"].RefersToRange.Formula = formula;
newSheet.Names["SouthProfit"].RefersToRange.Formula = formula;
newSheet.Names["EastProfit"].RefersToRange.Formula = formula;
newSheet.Names["WestProfit"].RefersToRange.Formula = formula;
// Move the new "Profit Report" sheet before the original "Sheet1".
newSheet.MoveBefore(Workbook.Worksheets["Sheet1"]);
}
}
}