NativeExcel2 for .NET Developer's Reference

Create new workbook

Example

This example creates new workbook with one sheet

[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)";  

         //Save workbook
         book.SaveAs("book.xls");                
     } 
 }
     
[Visual Basic]
 Imports System
 Imports NativeExcel
 
 Public 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 the 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)"

        '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)";  

     //Save workbook
     book->SaveAs(S"book.xls");                

     return 0;
 } // end main