How to: Use COM Interop to Create an Excel Spreadsheet (C# Programming Guide) 

The following code example illustrates how to use COM interop to create an Excel spreadsheet. For more information on Excel, see Microsoft Excel Objects, and Open Method

This example illustrates how to open an existing Excel spreadsheet in C# using .NET Framework COM interop capability. The Excel assembly is used to open and enter data into a range of cells in the Excel spreadsheet.


You must have Excel installed on your system for this code to run properly.


The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To create an Excel spreadsheet with COM interop

  1. Create a new C# console application in Visual Studio and call it CreateExcelWorksheet.

  2. Add the Excel assembly as a reference to the project: Right-click on the project, select Add Reference.

  3. Click the COM tab of the Add Reference dialog box, and find Microsoft Excel 11 Object Library.

  4. Double-click on Microsoft Excel 11 Object Library, and press OK.


    Depending on the version of Office installed the Excel Assembly may be called Excel 10 Object Library or Excel 11 Object Library.

  5. Copy the following code and paste over the contents of the Program.cs file.

    using System;
    using System.Reflection; 
    using Microsoft.Office.Interop.Excel;
    public class CreateExcelWorksheet
        static void Main()
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            if (xlApp == null)
                Console.WriteLine("EXCEL could not be started. Check that your office installation and project references are correct.");
            xlApp.Visible = true;
            Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
            Worksheet ws = (Worksheet)wb.Worksheets[1];
            if (ws == null)
                Console.WriteLine("Worksheet could not be created. Check that your office installation and project references are correct.");
            // Select the Excel cells, in the range c1 to c7 in the worksheet.
            Range aRange = ws.get_Range("C1", "C7");
            if (aRange == null)
                Console.WriteLine("Could not get a range. Check to be sure you have the correct versions of the office DLLs.");
            // Fill the cells in the C1 to C7 range of the worksheet with the number 6.
            Object[] args = new Object[1];
            args[0] = 6;
            aRange.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, aRange, args);
            // Change the cells in the C1 to C7 range of the worksheet to the number 8.
            aRange.Value2 = 8;


To use COM interop, you must have administrator or Power User security permissions. For more information on security, see .NET Framework Security.

See Also


How to: Use COM Interop to Check Spelling Using Word (C# Programming Guide)


C# Programming Guide
Interoperability Overview (C# Programming Guide)

Other Resources

Interoperability in the .NET Compact Framework
COM Interoperability Samples