+ Reply to Thread
Results 1 to 3 of 3

creating Excel Application Object in C#?

  1. #1
    SixSigmaGuy
    Guest

    creating Excel Application Object in C#?

    Can someone tell me how to create an Excel Application object in C#? I can
    create a Word and PowerPoint Application object, but when I try to create one
    for Excel, I get the build error:

    "Cannot create an instance of the abstract class or interface 'interface'"

    After adding the appropriate references, I use the following code to create
    a Word and PowerPoint Application object:

    Word.Application wdApp = new Word.Application();

    PowerPoint.Application ppApp = new PowerPoint.Application();

    But when I try it with Excel,

    Excel.Application xlApp = new Excel.Application();

    I get the build error I showed above.

    I've searched VS help and MSDN and haven't found any examples that do what
    I'm trying to do.

    I have references to:
    Excel
    Microsoft.Office.Core
    Office

    in my project. Is there some other reference I need?

    Thanks!

    BTW, I'm on a boat in the middle of the Atlantic for the next two weeks and
    I'm using a very slow satelitte connection that's costing me $42/hr. If I
    sent this to the wrong newsgroup, could you please forward it to the right
    one for me so I don't have to waste connect time? Thanks!


  2. #2

    Re: creating Excel Application Object in C#?

    I don't know C, BUT it does seem to be covered in various articles at

    http://support.microsoft.com

    for example
    http://support.microsoft.com/kb/302096/en-us
    How to automate Excel by using Visual C# to fill or to obtain data in a
    range by using arrays
    View products that this article applies to.
    Article ID : 302096
    Last Review : January 19, 2006
    Revision : 8.0
    This article was previously published under Q302096
    SUMMARY
    This article demonstrates how to automate Microsoft Excel by using
    Microsoft Visual C# 2005 or Microsoft Visual C# .NET to fill and
    retrieve values in a multi-cell range by using arrays.
    Back to the top

    MORE INFORMATION
    To fill a multi-cell range without populating cells one at a time, you
    can set the Value property of a Range object to a two-dimensional
    array. Likewise, you can retrieve a two-dimensional array of values for
    multiple cells at once by using the Value property. The following steps
    demonstrate this process for both setting and retrieving data using
    two-dimensional arrays.
    Build the Automation Client for Microsoft Excel
    1=2E Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET.
    2=2E On the File menu, click New, and then click Project. Select Windows
    Application from the Visual C# Project types. Form1 is created by
    default.
    3=2E Add a reference to Microsoft Excel 11.0 Object Library in Visual
    Studio 2005 or Microsoft Excel Object Library in Visual Studio .NET. To
    do this, follow these steps: a. On the Project menu, click Add
    Reference.
    b=2E On the COM tab, locate Microsoft Excel Object Library, and then
    click Select.

    In Visual Studio 2005, locate Microsoft Excel 11.0 Object Library on
    the COM tab.

    Note Microsoft Office 2003 includes Primary Interop Assemblies (PIAs).
    Microsoft Office XP does not include PIAs, but they may be downloaded.
    For additional information about Office XP PIAs, click the following
    article number to view the article in the Microsoft Knowledge Base:
    328912 (http://support.microsoft.com/kb/328912/) INFO: Microsoft Office
    XP PIAs Are Available for Download
    c=2E Click OK in the Add References dialog box to accept your
    selections. If you are prompted to generate wrappers for the libraries
    that you selected, click Yes.

    4=2E On the View menu, select Toolbox to display the Toolbox. Add two
    buttons and a check box to Form1.
    5=2E Set the Name and the Text properties for the check box to
    FillWithStrings.
    6=2E Double-click Button1. The code window for the form appears.
    7=2E In the code window, replace the following codeprivate void
    button1_Click(object sender, System.EventArgs e)
    {
    }

    with: //Declare these two variables globally so you can access
    them from both
    //Button1 and Button2.
    Excel.Application objApp;
    Excel._Workbook objBook;

    private void button1_Click(object sender, System.EventArgs e)
    {
    Excel.Workbooks objBooks;
    Excel.Sheets objSheets;
    Excel._Worksheet objSheet;
    Excel.Range range;

    try
    {
    // Instantiate Excel and start a new workbook.
    objApp =3D new Excel.Application();
    objBooks =3D objApp.Workbooks;
    objBook =3D objBooks.Add( Missing.Value );
    objSheets =3D objBook.Worksheets;
    objSheet =3D (Excel._Worksheet)objSheets.get_Item(1);

    //Get the range where the starting cell has the address
    //m_sStartingCell and its dimensions are m_iNumRows x
    m_iNumCols.
    range =3D objSheet.get_Range("A1", Missing.Value);
    range =3D range.get_Resize(5, 5);

    if (this.FillWithStrings.Checked =3D=3D false)
    {
    //Create an array.
    double[,] saRet =3D new double[5, 5];

    //Fill the array.
    for (long iRow =3D 0; iRow < 5; iRow++)
    {
    for (long iCol =3D 0; iCol < 5; iCol++)
    {
    //Put a counter in the cell.
    saRet[iRow, iCol] =3D iRow * iCol;
    }
    }

    //Set the range value to the array.
    range.set_Value(Missing.Value, saRet );
    }

    else
    {
    //Create an array.
    string[,] saRet =3D new string[5, 5];

    //Fill the array.
    for (long iRow =3D 0; iRow < 5; iRow++)
    {
    for (long iCol =3D 0; iCol < 5; iCol++)
    {
    //Put the row and column address in the cell.
    saRet[iRow, iCol] =3D iRow.ToString() + "|" +
    iCol.ToString();
    }
    }

    //Set the range value to the array.
    range.set_Value(Missing.Value, saRet );
    }

    //Return control of Excel to the user.
    objApp.Visible =3D true;
    objApp.UserControl =3D true;
    }
    catch( Exception theException )
    {
    String errorMessage;
    errorMessage =3D "Error: ";
    errorMessage =3D String.Concat( errorMessage,
    theException.Message );
    errorMessage =3D String.Concat( errorMessage, " Line: " );
    errorMessage =3D String.Concat( errorMessage,
    theException.Source );

    MessageBox.Show( errorMessage, "Error" );
    }
    }

    Note You must change the code in Visual Studio 2005. By default, Visual
    C# adds one form to the project when you create a Windows Forms
    project. The form is named Form1. The two files that represent the form
    are named Form1.cs and Form1.designer.cs. You write the code in
    Form1.cs. The Form1.designer.cs file is where the Windows Forms
    Designer writes the code that implements all the actions that you
    performed by dragging and dropping controls from the Toolbox.

    For more information about the Windows Forms Designer in Visual C#
    2005, visit the following Microsoft Developer Network (MSDN) Web site:
    http://msdn2.microsoft.com/en-us/library/ms173077.aspx
    (http://msdn2.microsoft.com/en-us/library/ms173077.aspx)
    8=2E Return to the design view for Form1, and double-click Button2.
    9=2E In the code window, replace the following codeprivate void
    button2_Click(object sender, System.EventArgs e)
    {
    }

    with: private void button2_Click(object sender, System.EventArgs e)
    {
    Excel.Sheets objSheets;
    Excel._Worksheet objSheet;
    Excel.Range range;

    try
    {
    try
    {
    //Get a reference to the first sheet of the workbook.
    objSheets =3D objBook.Worksheets;
    objSheet =3D (Excel._Worksheet)objSheets.get_Item(1);
    }

    catch( Exception theException )
    {
    String errorMessage;
    errorMessage =3D "Can't find the Excel workbook. Try
    clicking Button1 " +
    "to create an Excel workbook with data before running
    Button2.";

    MessageBox.Show( errorMessage, "Missing Workbook?");

    //You can't automate Excel if you can't find the data
    you created, so
    //leave the subroutine.
    return;
    }

    //Get a range of data.
    range =3D objSheet.get_Range("A1", "E5");

    //Retrieve the data from the range.
    Object[,] saRet;
    saRet =3D (System.Object[,])range.get_Value( Missing.Value );

    //Determine the dimensions of the array.
    long iRows;
    long iCols;
    iRows =3D saRet.GetUpperBound(0);
    iCols =3D saRet.GetUpperBound(1);

    //Build a string that contains the data of the array.
    String valueString;
    valueString =3D "Array Data\n";

    for (long rowCounter =3D 1; rowCounter <=3D iRows;
    rowCounter++)
    {
    for (long colCounter =3D 1; colCounter <=3D iCols;
    colCounter++)
    {

    //Write the next value into the string.
    valueString =3D String.Concat(valueString,
    saRet[rowCounter, colCounter].ToString() + ", ");
    }

    //Write in a new line.
    valueString =3D String.Concat(valueString, "\n");
    }

    //Report the value of the array.
    MessageBox.Show(valueString, "Array Values");
    }

    catch( Exception theException )
    {
    String errorMessage;
    errorMessage =3D "Error: ";
    errorMessage =3D String.Concat( errorMessage,
    theException.Message );
    errorMessage =3D String.Concat( errorMessage, " Line: " );
    errorMessage =3D String.Concat( errorMessage,
    theException.Source );

    MessageBox.Show( errorMessage, "Error" );
    }
    }


    10. Scroll to the top of the code window. Add the following line to the
    end of the list of using directives:using System.Reflection;
    using Excel =3D Microsoft.Office.Interop.Excel;



    Test the Automation Client
    1=2E Press F5 to build and to run the sample program.
    2=2E Click Button1. The program starts Microsoft Excel with a new
    workbook and populates cells A1:E5 of the first worksheet with numeric
    data from an array.
    3=2E Click Button2. The program retrieves the data in cells A1:E5 into a
    new array and displays the results in a message box.
    4=2E Select FillWithStrings, and then click Button1 to fill cells A1:E5
    with string data.

    Back to the top

    REFERENCES
    For more information, visit the following Microsoft Developer Network
    (MSDN) Web site:
    Microsoft Office Development with Visual Studio
    http://msdn.microsoft.com/library/en...sofficedev.asp
    (http://msdn.microsoft.com/library/en...sofficedev.asp)
    For additional information about using arrays to set and retrieve Excel
    data with earlier versions of Visual Studio, click the following
    article numbers to view the articles in the Microsoft Knowledge Base:
    186120 (http://support.microsoft.com/kb/186120/) How To Use MFC to
    Automate Excel and Fill a Range with an Array
    186122 (http://support.microsoft.com/kb/186122/) How To Use MFC to
    Automate Excel and Obtain an Array from a Range
    247412 (http://support.microsoft.com/kb/247412/EN-US/) INFO: Methods
    for Transferring Data to Excel from Visual Basic
    Back to the top


    ---------------------------------------------------------------------------=
    -----

    APPLIES TO
    =B7 Microsoft Visual C# 2005 Express Edition
    =B7 Microsoft Visual C# .NET 2003 Standard Edition
    =B7 Microsoft Visual C# .NET 2002 Standard Edition
    =B7 Microsoft Office Excel 2003
    =B7 Microsoft Excel 2002 Standard Edition

    Back to the top

    Keywords: kbhowto kbautomation KB302096=20

    Back to the top


  3. #3
    Chelvy
    Guest

    RE: creating Excel Application Object in C#?

    try adding reference to
    using Excel = Microsoft.Office.Interop.Excel;

    Here is my code:

    Excel.Application xlApp;
    Excel._Workbook xlWkBk;
    Excel.Workbooks xlBooks;

    xlApp = new Excel.Application();

    //xlBooks = new Excel.Workbooks();
    xlWkBk = xlApp.Workbooks.Open(textBox1.Text, 0, true, 5,
    System.Reflection.Missing.Value, System.Reflection.Missing.Value, true,
    Excel.XlPlatform.xlWindows, System.Reflection.Missing.Value, false, false,
    System.Reflection.Missing.Value, false, true,
    Excel.XlCorruptLoad.xlNormalLoad);


    Connection Strings:
    http://www.connectionstrings.com/
    Accessing Excel Spreadsheet in C#
    http://www.c-sharpcorner.com//Code/2...essExcelDb.asp
    How To Use ADO.NET to Retrieve and Modify Records in an Excel Workbook With
    Visual Basic .NET
    http://support.microsoft.com/?kbid=316934
    The C# Station ADO.NET Tutorial
    http://www.csharp-station.com/Tutori.../Lesson01.aspx
    Opening and Navigating Excel with C#
    http://www.c-sharpcorner.com/winforms/ExcelReadMG.asp
    http://www.codeproject.com/csharp/csharp_excel.asp
    Transforming excel Pivot tables into ADO.NET web forms
    http://www.codeguru.com/forum/showth...hreadid=384375
    Pivot function (C#)
    http://weblogs.sqlteam.com/jeffs/articles/5091.aspx
    Understanding the Excel Object Model from a .NET Developer's Perspective
    http://msdn.microsoft.com/library/de...e&hidetoc=true
    Reading and Writing Excel using OLEDB
    http://www.codeproject.com/csharp/Excel_using_OLEDB.asp

    "SixSigmaGuy" wrote:

    > Can someone tell me how to create an Excel Application object in C#? I can
    > create a Word and PowerPoint Application object, but when I try to create one
    > for Excel, I get the build error:
    >
    > "Cannot create an instance of the abstract class or interface 'interface'"
    >
    > After adding the appropriate references, I use the following code to create
    > a Word and PowerPoint Application object:
    >
    > Word.Application wdApp = new Word.Application();
    >
    > PowerPoint.Application ppApp = new PowerPoint.Application();
    >
    > But when I try it with Excel,
    >
    > Excel.Application xlApp = new Excel.Application();
    >
    > I get the build error I showed above.
    >
    > I've searched VS help and MSDN and haven't found any examples that do what
    > I'm trying to do.
    >
    > I have references to:
    > Excel
    > Microsoft.Office.Core
    > Office
    >
    > in my project. Is there some other reference I need?
    >
    > Thanks!
    >
    > BTW, I'm on a boat in the middle of the Atlantic for the next two weeks and
    > I'm using a very slow satelitte connection that's costing me $42/hr. If I
    > sent this to the wrong newsgroup, could you please forward it to the right
    > one for me so I don't have to waste connect time? Thanks!
    >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1