NativeExcel for .Net Developer's Reference

Factory.CreateWorkbookMethod

Creates an instance of IWorkbook.

[Visual Basic]
SharedPublicFunctionCreateWorkbook()AsIWorkbook
[C#]
publicstaticIWorkbookCreateWorkbook();
[C++]
public:staticIWorkbook*CreateWorkbook();
[JScript]
publicstaticfunctionCreateWorkbook():IWorkbook;

Return Value

Returns IWorkbook interface.

Example

[C#]
 using System; 
 using NativeExcel; 

 class Program { 
     static void Main(string[] args) { 
         //Create a new empty workbook. 
         IWorkbook book = NativeExcel.Factory.CreateWorkbook(); 
         //Create a new sheet in the workbook. 
         IWorksheet sheet = book.Worksheets.Add(); 
         //Set a value for cell A1
         sheet.Cells["A1"].Value = 300;  
         //Set a value for cell A2
         sheet.Cells[2,1].Value = 200;  
         //Set a formula for cell A3
         sheet.Cells["A3"].Formula = "=SUM(A1:A2)";  
         //Output the result of the formula. 
         System.Console.WriteLine("{0} = {1}", 
              sheet.Cells["A3"].Formula, 
              sheet.Cells["A3"].Value);
         //Save workbook
         book.SaveAs("book.xls");                
     } 
 }
 
[VisualBasic]
Imports System
Imports NativeExcel

Module Program
    Sub Main()
        ' Create a new empty workbook. 
        Dim book As IWorkbook = NativeExcel.Factory.CreateWorkbook()
        'Create a new sheet in the workbook. 
        Dim sheet As IWorksheet = book.Worksheets.Add()
        'Set a value for cell A1
        sheet.Cells("A1").Value = 300
        'Set a value for cell A2
        sheet.Cells(2,1).Value = 200
        'Set a formula for cell A3
        sheet.Cells("A3").Formula = "=SUM(A1:A2)"
        'Output the result of the formula. 
        Console.WriteLine("{0} = {1}", _
             sheet.Cells("A3").Formula, _
             sheet.Cells("A3").Value)
        'Save workbook
        book.SaveAs("book.xls")                
    End Sub 
End Module
[C++]
 #using <System.dll>
 using namespace System; 
 
 #using <NativeExcel.dll>
 using namespace NativeExcel; 

 int main() {
     //Create a new empty workbook. 
     IWorkbook* book = NativeExcel::Factory::CreateWorkbook(); 
     //Create a new sheet in the workbook. 
     IWorksheet* sheet = book->Worksheets->Add(); 
     //Set a value for cell A1
     sheet->Cells->Item[S"A1"]->Value = 300;  
     //Set a value for cell A2
     sheet->Cells->Item[2,1]->Value = 200;  
     //Set a formula for cell A3
     sheet->Cells->Item[S"A3"]->Formula = S"=SUM(A1:A2)";  
     //Output the result of the formula. 
     Console::WriteLine(S"{0} = {1}", 
              sheet->Cells->Item[S"A3"]->Formula,
              sheet->Cells->Item[S"A3"]->Value);
     //Save workbook
     book->SaveAs(S"book.xls");                

     return 0;
 } // end main
 

See Also

Factory Class | NativeExcel Namespace | IWorkbook Interface