How To_ Use COM Interop to Create an Excel Spreadsheet (C#)

Embed Size (px)

Citation preview

  • 7/28/2019 How To_ Use COM Interop to Create an Excel Spreadsheet (C#)

    1/3

    How to: Use COM Interop to Create anExcel 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 interop1. 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 Exce l 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.

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

    Visual Studio 2005 19 out of 51 rated this helpful

    using System;

    using System.Reflection;

    using Microsoft.Office.Interop.Excel;

    C#

    http://msdn.microsoft.com/en-us/library/zbhkx167(v=vs.80).aspxhttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaxl10/html/xlmthOpen.asphttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaxl10/html/xlmthOpen.asp
  • 7/28/2019 How To_ Use COM Interop to Create an Excel Spreadsheet (C#)

    2/3

    /12/12 How to: Use COM Interop to Create an Excel Spreadsheet (C#)

    sdn.microsof t.com/ en-us/library/m s173186(v=v s.80).aspx

    SecurityTo use COM interop, you must have administrator or Power User security permissions. For more information on security,

    see .NET Framework Security.

    See Also

    publicclass CreateExcelWorksheet

    {

    staticvoid 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 numbe

    r 8.

    aRange.Value2 = 8;

    }

    }

    http://www.msdn.microsoft.com/security/securecode/dotnet/default.aspx
  • 7/28/2019 How To_ Use COM Interop to Create an Excel Spreadsheet (C#)

    3/3

    /12/12 How to: Use COM Interop to Create an Excel Spreadsheet (C#)

    sdn.microsof t.com/ en-us/library/m s173186(v=v s.80).aspx

    Community Additions

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

    ConceptsC# Programming Guide

    Interoperability Overview (C# Programming Guide)

    Other ResourcesInteroperability in the .NET Compact Framework

    COM Interoperability Samples

    Missing reference

    The proposed Microsoft Exce l 11 Object Library is not enough.

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

    For current documention on interoperability, see http://msdn.microsoft.com/en-us/library/ms173184.aspx.

    RobinR-H

    5/4/2010

    Specify CultureInfo

    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

    M.L. Somers

    6/29/2009

    2012 Microsoft. All rights reserved.

    http://social.msdn.microsoft.com/profile/m.l.%20somers/http://social.msdn.microsoft.com/profile/robinr-h/http://msdn.microsoft.com/en-us/library/cxcz83xf(v=vs.80).aspxhttp://msdn.microsoft.com/en-us/library/k3f1t3ct(v=vs.80).aspxhttp://msdn.microsoft.com/en-us/library/ms173185(v=vs.80).aspxhttp://msdn.microsoft.com/en-us/library/67ef8sbd(v=vs.80).aspxhttp://msdn.microsoft.com/en-us/library/ms173188(v=vs.80).aspxhttp://social.msdn.microsoft.com/profile/m.l.%20somers/http://social.msdn.microsoft.com/profile/robinr-h/