SpreadsheetGear The Performance Spreadsheet Component Company 
Add Method
See Also  Example
SpreadsheetGear Namespace > IWorksheets Interface : Add Method
Returns a newly created worksheet which is added to the end of the worksheet collection.

Syntax

C# 
virtual IWorksheet Add()

Return Value

The newly created worksheet.

Example

C#Copy Code
class Program 

    static void Main() 
    { 
        // Create a new empty workbook set. 
        SpreadsheetGear.IWorkbookSet workbookSet = SpreadsheetGear.Factory.GetWorkbookSet(); 
        // Create a new empty workbook in the workbook set. 
        SpreadsheetGear.IWorkbook workbook = workbookSet.Workbooks.Add(); 
        // Add a second worksheet. 
        workbook.Worksheets.Add(); 
        // Rename worksheet "Sheet1" to "Hello" 
        workbook.Worksheets["Sheet1"].Name = "Hello"; 
        // Rename worksheet "Sheet2" to "World" 
        workbook.Worksheets["Sheet2"].Name = "World"; 
        // Save to C:\MyData.xls. 
        workbook.SaveAs("C:\\MyData.xls", SpreadsheetGear.FileFormat.XLS97); 
        // Close the workbook, removing it from the workbook set. 
        workbookSet.Workbooks.Close(); 
        // Open the saved workbook from memory. 
        workbook = workbookSet.Workbooks.Open("C:\\MyData.xls"); 
        // Output the sheet names separated by a space ("Hello World"). 
        System.Console.WriteLine(workbook.Worksheets[0].Name + " " + workbook.Worksheets[1].Name); 
    } 

    
Visual BasicCopy Code
Class Program
    Shared Sub Main()
        ' Create a new empty workbook set.
        Dim workbookSet As SpreadsheetGear.IWorkbookSet = SpreadsheetGear.Factory.GetWorkbookSet()
        ' Create a new empty workbook in the workbook set.
        Dim workbook As SpreadsheetGear.IWorkbook = workbookSet.Workbooks.Add()
        ' Add a second worksheet.
        workbook.Worksheets.Add()
        ' Rename worksheet "Sheet1" to "Hello"
        workbook.Worksheets("Sheet1").Name = "Hello"
        ' Rename worksheet "Sheet2" to "World"
        workbook.Worksheets("Sheet2").Name = "World"
        ' Save to C:\MyData.xls.
        workbook.SaveAs("C:\MyData.xls", SpreadsheetGear.FileFormat.XLS97)
        ' Close the workbook, removing it from the workbook set.
        workbookSet.Workbooks.Close()
        ' Open the saved workbook from memory.
        workbook = workbookSet.Workbooks.Open("C:\MyData.xls")
        ' Output the sheet names separated by a space ("Hello World").
        System.Console.WriteLine((workbook.Worksheets(0).Name + " " + workbook.Worksheets(1).Name))
    End Sub 'Main
End Class 'Program

Remarks

The new worksheet is created as the last worksheet in the workbook.

Requirements

Platforms: Windows 98, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family. SpreadsheetGear for .NET 1.x requires the Microsoft .NET Framework 1.1 or .NET 2.0. SpreadsheetGear for .NET 2006 requires the Microsoft .NET Framework 2.0.

See Also