Use Excel as a Component in .NET

Another way for Excel to interact with the .NET world is to program with Excel objects directly in VB.NET. In this case, Excel becomes a component for use in a .NET application—the reverse of the case just shown.

Using Excel as a component in a .NET application is handy when you want to present application output using the Excel interface—as a spreadsheet or chart, for instance.

Note

Excel works as a component, too. It’s a big one, though, and you need to be carefulwhen using it from .NET applications.

How to do it

To create a .NET application that uses Excel as a component:

  1. Create a new Windows application project in Visual Studio .NET.

  2. Choose Project Add Reference. Visual Studio .NET displays the Add Reference dialog box, click the COM tab. Visual Studio .NET displays the COM objects registered on your system as shown in Figure 5-10.

    Adding a reference to the Microsoft Excel object library

    Figure 5-10. Adding a reference to the Microsoft Excel object library

  3. Select the Microsoft Excel 11.0 Object Library and click Select, then OK to add the reference to your project. Visual Studio .NET automatically references the PIA for the Excel object library if it is installed on your system.

  4. If the PIA is not installed, Visual Studio .NET creates a new interop assembly and adds it to your project (this is not what you want—the PIA is much more reliable). To make sure you are using the PIA, check the Name ...

Get Excel 2003 Programming: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.