Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 244
Default 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. Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET.
2. 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. 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. 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. 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. On the View menu, select Toolbox to display the Toolbox. Add two
buttons and a check box to Form1.
5. Set the Name and the Text properties for the check box to
FillWithStrings.
6. Double-click Button1. The code window for the form appears.
7. 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 = new Excel.Application();
objBooks = objApp.Workbooks;
objBook = objBooks.Add( Missing.Value );
objSheets = objBook.Worksheets;
objSheet = (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 = objSheet.get_Range("A1", Missing.Value);
range = range.get_Resize(5, 5);

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

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

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

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

//Fill the array.
for (long iRow = 0; iRow < 5; iRow++)
{
for (long iCol = 0; iCol < 5; iCol++)
{
//Put the row and column address in the cell.
saRet[iRow, iCol] = 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 = true;
objApp.UserControl = true;
}
catch( Exception theException )
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat( errorMessage,
theException.Message );
errorMessage = String.Concat( errorMessage, " Line: " );
errorMessage = 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. Return to the design view for Form1, and double-click Button2.
9. 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 = objBook.Worksheets;
objSheet = (Excel._Worksheet)objSheets.get_Item(1);
}

catch( Exception theException )
{
String errorMessage;
errorMessage = "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 = objSheet.get_Range("A1", "E5");

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

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

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

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

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

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

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

catch( Exception theException )
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat( errorMessage,
theException.Message );
errorMessage = String.Concat( errorMessage, " Line: " );
errorMessage = 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 = Microsoft.Office.Interop.Excel;



Test the Automation Client
1. Press F5 to build and to run the sample program.
2. 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. Click Button2. The program retrieves the data in cells A1:E5 into a
new array and displays the results in a message box.
4. 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
· Microsoft Visual C# 2005 Express Edition
· Microsoft Visual C# .NET 2003 Standard Edition
· Microsoft Visual C# .NET 2002 Standard Edition
· Microsoft Office Excel 2003
· Microsoft Excel 2002 Standard Edition

Back to the top

Keywords: kbhowto kbautomation KB302096

Back to the top

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default 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 Perspectiv
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
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
cannot start the source application for this object in excel Philip Mollica Charts and Charting in Excel 3 May 27th 06 02:46 AM
Excel Automation Add-In, Accessing the application object smedcalf Excel Programming 1 March 10th 05 09:10 PM
"Excel.Application" Object Model Shaker Charts and Charting in Excel 2 January 9th 05 10:24 AM
Problem on the ASP using the Excel.Application Object David Huang Excel Programming 0 April 23rd 04 02:07 AM
Diplaying Excel Application through Object Model Bob Phillips[_5_] Excel Programming 1 August 13th 03 03:39 PM


All times are GMT +1. The time now is 04:16 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"