Other API Samples

Live SpreadsheetGear API Samples

Worksheet Samples Move and Copy Sheets

Sheets can be moved and copied with ISheet.MoveAfter(...) / MoveBefore(...) and CopyAfter(...) / CopyBefore(...).

// Open template workbook
SpreadsheetGear.IWorkbook template = SpreadsheetGear.Factory.GetWorkbook("WorkbookTemplate.xlsx");

// Create a new workbook for final report workbook.
SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
            
// Make a copy of template sheet in new workbook and rename sheet.
SpreadsheetGear.IWorksheet newSheet = template.Worksheets["Template Sheet"].CopyAfter(workbook.ActiveWorksheet) as SpreadsheetGear.IWorksheet;
newSheet.Name = "Profit Report";

// Populate new sheet with some test 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 sheet before the original "Sheet1" that was created.
newSheet.MoveBefore(workbook.Worksheets["Sheet1"]);
Run This Sample
Download File

Download an Excel Open XML Workbook (*.xlsx) file with the results of this sample.


Supporting Files

The following files are utilized by this sample: