Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Visual C#
Interoperability
 How to: Use COM Interop to Create a...
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
C# Programming Guide
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.

NoteNote

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

NoteNote

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.

    NoteNote

    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.

    C#
    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.");
                return;
            }
            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;
        }
    }

Security

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

See Also

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Missing reference      cguillemette   |   Edit   |   Show History
The proposed Microsoft Excel 11 Object Library is not enough.

The reference library Microsoft.Office.Interop.Excel needs to be added also.

Specify CultureInfo      M.L. Somers   |   Edit   |   Show History
Before adding the workbook, you should specify the culture, else it will throw an "Old format or invalid type library" error on non-English machines:

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

See http://support.microsoft.com/kb/320369

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker