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.
Note
You must have Excel installed on your system for this code to run properly.
Note
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
Create a new C# console application in Visual Studio and call it CreateExcelWorksheet.
Add the Excel assembly as a reference to the project: Right-click on the project, select Add Reference.
Click the COM tab of the Add Reference dialog box, and find Microsoft Excel 11 Object Library.
Double-click on Microsoft Excel 11 Object Library, and press OK.
Note
Depending on the version of Office installed the Excel Assembly may be called Excel 10 Object Library or Excel 11 Object Library.
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."); 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
Tasks
How to: Use COM Interop to Check Spelling Using Word (C# Programming Guide)
Concepts
C# Programming Guide
Interoperability Overview (C# Programming Guide)
Other Resources
Interoperability in the .NET Compact Framework
COM Interoperability Samples