NativeExcel2 for .NET Developer's Reference

Open an existing workbook

Example

This example opens an existing workbook book.xls

[C#]
 using System; 
 using NativeExcel; 

 class Program { 
     static void Main(string[] args) { 
         //Open workbook book.xls 
         IWorkbook book = NativeExcel.Factory.OpenWorkbook("book.xls"); 

         if (book != null) {
             //sheet one  
             IWorksheet sheet = book.Worksheets[1]; 
 
             //Change a value for cell A1
             sheet.Cells["A1"].Value = 300;  
             //Change 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()
         'Open workbook book.xls 
         Dim book As IWorkbook = NativeExcel.Factory.OpenWorkbook("book.xls") 

         If Not book Is Nothing Then
             'sheet one  
             Dim sheet As IWorksheet = book.Worksheets(1) 
 
             '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 If
     End Sub

 End Module
     
[C++]
 #using <System.dll>
 using namespace System; 

 #using <NativeExcel.dll>
 using namespace NativeExcel; 

 int main() {
     //Open book.xls
     IWorkbook* book = NativeExcel::Factory::OpenWorkbook(S"book.xls"); 

     if (book != null) {
         //Get sheet one 
         IWorksheet* sheet = book->Worksheets->Item[1]; 

         //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