Creating an Excel Spreadsheet Programmatically

Introduction

The Interoperability services make it very easy to work with COM Capable Applications such as Word and Excel. This article reveals using Excel from a managed application. Excel is the spreadsheet component of Microsoft Office 2000. The majority of Excel programmatic functionality is exposed through Automation via the type library Excel9.olb. The intention of this article is to express that a managed application can interrelate with Excel as a COM server.

The first step is to create a reference in our project to Excel 9.0 Objects Library. By using Tlbimp tool we can generate Excel.dll.

TlbImp Excel9.olb Excel.dll

By adding Excel.dll to our program we can use the functionality of the Excel.

Now let us see in detail how to create an Excel Spreadsheet? & Set values to the cell using C#. The codes for Creating, make visible, add a new workbook and to set a value for cell in the Excel file is shown below.

  1. Creating new excel.application
    1. Application exc = new Application();  
    2. if (exc == null)   
    3. {  
    4.     Console.WriteLine("ERROR: EXCEL couldn't be started");  
    5.     return 0;  
    6. }
  2. To make application visible
    1. exc.set_Visible(0, true);
  3. To get the workbooks collection
    1. Workbooks workbooks = exc.Workbooks;  
    2. _Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet, 0);
  4. To get the worksheets collection
    1. _Worksheet worksheet = (_Worksheet) sheets.get_Item(1);  
    2. if (worksheet == null)   
    3. {  
    4.     Console.WriteLine ("ERROR in worksheet == null");  
    5. }
  5. To set the value for cell
    1. Range range1 = worksheet.get_Range("C1", Missing.Value);  
    2. if (range1 == null)   
    3. {  
    4.     Console.WriteLine ("ERROR: range == null");  
    5. }  
    6. const int nCells = 1;  
    7. Object[] args1 = new Object[1];  
    8. args1[0] = nCells;  
    9. range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, range1, args1);

Example

  1. using System;  
  2. using System.Reflection; // For Missing.Value and BindingFlags  
  3. using System.Runtime.InteropServices; // For COMException  
  4. using Excel;  
  5. class AutoExcel  
  6. {  
  7.     public static int Main()  
  8.     {  
  9.         Application exc = new Application();  
  10.         if (exc == null)  
  11.         {  
  12.             Console.WriteLine("ERROR: EXCEL couldn't be started!");  
  13.             return 0;  
  14.         }  
  15.         exc.set_Visible(0, true);  
  16.         Workbooks workbooks = exc.Workbooks;  
  17.         _Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet, 0);  
  18.         Sheets sheets = workbook.Worksheets;  
  19.         _Worksheet worksheet = (_Worksheet)sheets.get_Item(1);  
  20.         if (worksheet == null)  
  21.         {  
  22.             Console.WriteLine("ERROR: worksheet == null");  
  23.         }  
  24.         Range range1 = worksheet.get_Range("C1", Missing.Value);  
  25.         if (range1 == null)  
  26.         {  
  27.             Console.WriteLine("ERROR: range == null");  
  28.         }  
  29.         const int nCells = 1;  
  30.         Object[] args1 = new Object[1];  
  31.         args1[0] = nCells;  
  32.         range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, range1, args1);  
  33.         return 100;  
  34.     }  
  35. }  
Now let us observe how to send a single dimension array to Excel:

It is similar to set the value for the cell. Only change is we use array as args2[0] = array2.

  1. const int nCell = 5;  
  2. Range range2 = worksheet.get_Range("A1""E1");  
  3. int[] array2 = new int [nCell];  
  4. for (int i=0; i < array2.GetLength(0); i++)   
  5. {  
  6.     array2[i] = i+1;  
  7. }  
  8. Object[] args2 = new Object[1];  
  9. args2[0] = array2;  
  10. range2.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, range2, args2);  

Output



Conclusion

With the help of TlbImp.exe tool we can generate .NET assembly from Type library files and we can use that functionality of Type library file in C#.


Similar Articles