473,387 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

CSharp and Excel

I am looking to take data from an excel spreadsheet into a csharp
application to manipulate the data in various ways. Currently, I am
using VS2005 (self-taught C#) and Excel 2000. I have researched into
using the Excel.Application object and have successfully openned a
worksheet in Excel (though hidden from the user).

The problem now comes when trying to get the data off of a spreadsheet.
When I try to create an instance of the worksheet, it fails:

oXL.Sheets["Sheet1"].Activate();

And even if I get that to work, I don't know how to pull the data off
of the spread sheet. I understand that I will need to get the range,
which I can do. Anyways, here is what I have so far:

oXL = new Excel.Application();
oWB = (Excel._Workbook)(oXL.Workbooks.Open(@"C:\workbook .xls",
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing));
oXL.Sheets("Sheet1").Activate(); //Doesn't compile
Excel.Worksheets tmpWBs = oXL.Sheets;
Excel.Worksheet tmpWB = tmpWBs["Sheet1"];
tmpWB.Activate();
stringTemp = (string)tmpWB.Cells[1,1];
// Rest of code

Any help is greatly appreciated.

Dec 30 '06 #1
6 14790
First, you need to get to the Worksheet object through
the Workbook object, not through the Application object.

Second, you need to cast it to an ExcelWorksheet type
in order to activate it.

This is how to do it in VB2005. (Yes, I know it's a C# ng,
please don't castigate me -- isn't VB better than nothing?)

xlApp is my Excel.Application object.
FinalWkBk is the index into the workbooks collection, and
I'm using WorkSheets(1) (it's the first one in the workbook;
this is not 0-based).

Dim xlTwo As Excel.Worksheet = _
CType(xlApp.Workbooks(FinalWkBk).WorkSheets(1), Excel.WorkSheet)
xlTwo.Activate()

I think that translates to this, but I'm posting the VB code in
case I'm wrong, so everybody can correct me.

Excel.Worksheet xlTwo = _
Excel.Worksheet(xlApp.Workbooks(FinalWkBk).WorkShe ets(1))
xlTwo.Activate()

So for you, this would probably be

oXL = new Excel.Application()
oWB = (Excel._Workbook)(...blahblahblah)

Excel.Worksheet oSheet = _
Excel.Worksheet(oWB.WorkSheets("Sheet1"))
oSheet.Activate()
Good luck.
Robin S.
--------------------------
<gu**********@gmail.comwrote in message
news:11*********************@v33g2000cwv.googlegro ups.com...
>I am looking to take data from an excel spreadsheet into a csharp
application to manipulate the data in various ways. Currently, I am
using VS2005 (self-taught C#) and Excel 2000. I have researched into
using the Excel.Application object and have successfully openned a
worksheet in Excel (though hidden from the user).

The problem now comes when trying to get the data off of a
spreadsheet.
When I try to create an instance of the worksheet, it fails:

oXL.Sheets["Sheet1"].Activate();

And even if I get that to work, I don't know how to pull the data off
of the spread sheet. I understand that I will need to get the range,
which I can do. Anyways, here is what I have so far:

oXL = new Excel.Application();
oWB = (Excel._Workbook)(oXL.Workbooks.Open(@"C:\workbook .xls",
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing));
oXL.Sheets("Sheet1").Activate(); //Doesn't compile
Excel.Worksheets tmpWBs = oXL.Sheets;
Excel.Worksheet tmpWB = tmpWBs["Sheet1"];
tmpWB.Activate();
stringTemp = (string)tmpWB.Cells[1,1];
// Rest of code

Any help is greatly appreciated.

Dec 30 '06 #2
My comment won't solve your problem, however, i just want to pass on the
some wisdom having dealt with Office and other interop.

If you want to communicate with legacy COM objects, such as Office or
DAO or RDO, do yourself a favor and use VB2005. Office objects have a
ton of optional parameters (as your code snippet illustrates) in pretty
much every single method call. So unless, you want to waste your time
making sure that you passed in enough Type.Missing statements, just use
VB2005's ability to use optional parameters.

Regards
gu**********@gmail.com wrote:
I am looking to take data from an excel spreadsheet into a csharp
application to manipulate the data in various ways. Currently, I am
using VS2005 (self-taught C#) and Excel 2000. I have researched into
using the Excel.Application object and have successfully openned a
worksheet in Excel (though hidden from the user).

The problem now comes when trying to get the data off of a spreadsheet.
When I try to create an instance of the worksheet, it fails:

oXL.Sheets["Sheet1"].Activate();

And even if I get that to work, I don't know how to pull the data off
of the spread sheet. I understand that I will need to get the range,
which I can do. Anyways, here is what I have so far:

oXL = new Excel.Application();
oWB = (Excel._Workbook)(oXL.Workbooks.Open(@"C:\workbook .xls",
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing));
oXL.Sheets("Sheet1").Activate(); //Doesn't compile
Excel.Worksheets tmpWBs = oXL.Sheets;
Excel.Worksheet tmpWB = tmpWBs["Sheet1"];
tmpWB.Activate();
stringTemp = (string)tmpWB.Cells[1,1];
// Rest of code

Any help is greatly appreciated.
Dec 30 '06 #3
<gu**********@gmail.comwrote in message
news:11*********************@v33g2000cwv.googlegro ups.com...
oXL.Sheets("Sheet1").Activate(); //Doesn't compile
oXL.Sheets["Sheet1"].Activate();
Dec 30 '06 #4
I agree. Having done Excel programming from c# before I wish I would have
started the project in VB.NET.

There is a managed library (it costs but depending on the size of the
project could save you time and money) for doing spreadsheet work
(http://www.aspose.com/).

Another thing to note is if you are wanting to do Office interop from an
ASP.NET website don't bother, office isn't designed to support it (altho it
is possible). Go for the aspose approach above.

Luke
http://blog.lukesmith.net

"Frank Rizzo" <no**@none.comwrote in message
news:uP*************@TK2MSFTNGP02.phx.gbl...
My comment won't solve your problem, however, i just want to pass on the
some wisdom having dealt with Office and other interop.

If you want to communicate with legacy COM objects, such as Office or DAO
or RDO, do yourself a favor and use VB2005. Office objects have a ton of
optional parameters (as your code snippet illustrates) in pretty much
every single method call. So unless, you want to waste your time making
sure that you passed in enough Type.Missing statements, just use VB2005's
ability to use optional parameters.

Regards
gu**********@gmail.com wrote:
>I am looking to take data from an excel spreadsheet into a csharp
application to manipulate the data in various ways. Currently, I am
using VS2005 (self-taught C#) and Excel 2000. I have researched into
using the Excel.Application object and have successfully openned a
worksheet in Excel (though hidden from the user).

The problem now comes when trying to get the data off of a spreadsheet.
When I try to create an instance of the worksheet, it fails:

oXL.Sheets["Sheet1"].Activate();

And even if I get that to work, I don't know how to pull the data off
of the spread sheet. I understand that I will need to get the range,
which I can do. Anyways, here is what I have so far:

oXL = new Excel.Application();
oWB = (Excel._Workbook)(oXL.Workbooks.Open(@"C:\workbook .xls",
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing));
oXL.Sheets("Sheet1").Activate(); //Doesn't compile
Excel.Worksheets tmpWBs = oXL.Sheets;
Excel.Worksheet tmpWB = tmpWBs["Sheet1"];
tmpWB.Activate();
stringTemp = (string)tmpWB.Cells[1,1];
// Rest of code

Any help is greatly appreciated.
Dec 30 '06 #5
@ Robin:
I'll give that a try and let you know how it goes. Plus I'll start
working on the VB code too (see comment in @ Frank)

@ Frank:
My initial goal is to create the same application in C# and VB just to
gain the experience in both. So even if I do create the VB app, I will
have only delayed my progress with C#. And I know that C# is more
picky about parameters but I figure that if I use C# first, I'll
understand the functions better than if I use VB first. I will be
using VB at some time though. Would you suggest doing both
applications parallel to each other such that I can test out with VB if
C# doesn't work?

@ Mark:
I had updated it that problem in another version of the code that isn't
readily accessible at the moment. Even that code didn't compile:

oXL.Sheets["Sheet1"].Activate();

@ Luke:
While that product looks really great, I do not think I will use it as
this is simply a hobby project and not for an actual company, and being
a college student... well hope you understand. This is simply a
windows app and not a website, if I do that, I probably would interact
with Google's spreadsheet instead. Thanks for the tips

On Dec 29, 9:52 pm, "Luke Smith" <stuffNOSPAM@lukesmithDOTnetwrote:
I agree. Having done Excel programming from c# before I wish I would have
started the project in VB.NET.

There is a managed library (it costs but depending on the size of the
project could save you time and money) for doing spreadsheet work
(http://www.aspose.com/).

Another thing to note is if you are wanting to do Office interop from an
ASP.NET website don't bother, office isn't designed to support it (altho it
is possible). Go for the aspose approach above.

Lukehttp://blog.lukesmith.net

"Frank Rizzo" <n...@none.comwrote in messagenews:uP*************@TK2MSFTNGP02.phx.gbl.. .
My comment won't solve your problem, however, i just want to pass on the
some wisdom having dealt with Office and other interop.
If you want to communicate with legacy COM objects, such as Office or DAO
or RDO, do yourself a favor and use VB2005. Office objects have a ton of
optional parameters (as your code snippet illustrates) in pretty much
every single method call. So unless, you want to waste your time making
sure that you passed in enough Type.Missing statements, just use VB2005's
ability to use optional parameters.
Regards
gumbystat...@gmail.com wrote:
I am looking to take data from an excel spreadsheet into a csharp
application to manipulate the data in various ways. Currently, I am
using VS2005 (self-taught C#) and Excel 2000. I have researched into
using the Excel.Application object and have successfully openned a
worksheet in Excel (though hidden from the user).
The problem now comes when trying to get the data off of a spreadsheet.
When I try to create an instance of the worksheet, it fails:
oXL.Sheets["Sheet1"].Activate();
And even if I get that to work, I don't know how to pull the data off
of the spread sheet. I understand that I will need to get the range,
which I can do. Anyways, here is what I have so far:
oXL = new Excel.Application();
oWB = (Excel._Workbook)(oXL.Workbooks.Open(@"C:\workbook .xls",
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing));
oXL.Sheets("Sheet1").Activate(); //Doesn't compile
Excel.Worksheets tmpWBs = oXL.Sheets;
Excel.Worksheet tmpWB = tmpWBs["Sheet1"];
tmpWB.Activate();
stringTemp = (string)tmpWB.Cells[1,1];
// Rest of code
Any help is greatly appreciated.- Hide quoted text -- Show quoted text -
Dec 30 '06 #6
Hi,

gu**********@gmail.com wrote:
I am looking to take data from an excel spreadsheet into a csharp
application to manipulate the data in various ways. Currently, I am
using VS2005 (self-taught C#) and Excel 2000. I have researched into
using the Excel.Application object and have successfully openned a
worksheet in Excel (though hidden from the user).
Just want to point that you can also access Excel using ADO.NET. If your
application does nothing but read data, my guess is that the code would
be easier. Additionally, you don't need to have Excel installed on the
target machine, only ADO.NET.

See this:
http://geekswithblogs.net/lbugnion/a.../25/89315.aspx

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Jan 1 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jonathan Grobe | last post by:
From: Nigel Perry <nigel@cosc.canterbury.ac.nz> Newsgroups: news.announce.newgroups,news.groups Subject: RFD: comp.std.csharp Date: Tue, 08 Jul 2003 00:26:01 +0000 REQUEST FOR DISCUSSION (RFD)...
7
by: Peter Smirnov | last post by:
Sorry for this newbie question but as far as I heard one need at least VisualStudio to develop CSharp applications. Is this correct? Are there otherwise some command line tools like javac.exe and...
0
by: josephdomus | last post by:
Successfull Internet and Direct Marketing products on www.promotionsite.net * NEW * DOMUS Domains Toolkit Fall 2004 - Unique on the Net 4 Millions "Whois" Domains data with Expiration Date and...
2
by: news.microsoft.com | last post by:
Hi: I work in Csharp's parser files by LEX/YACC.Now I have only CSharp-lex.l and CSharp.y file,but they not have CSharp'comment Parse. this is a part of CSharp-lex.l. ........................
1
by: Grzegorz Kaczor | last post by:
Hello, I encountered a strange problem. I've got a Windows application written in C# that fails with StackOverflowException when assigning to a property that is defined in superclass (the...
7
by: Alan Roberts | last post by:
Can someone please explain the following for me... I am trying to link to a .NET DLL from Excel. Excel needs to pass a reference to itself to the DLL and then the DLL needs to perform some work...
0
by: akantrowitz | last post by:
Can you replicate the Excel solver functionality from within Csharp? thks, ak
2
by: Karl | last post by:
Hi all, I'm reasonably new to csharp so you have to forgive me asking what may be a stupid question... As I said, I'm new to CSharp but in VB I was able to click anywhere on a DataGridView...
1
atksamy
by: atksamy | last post by:
Hi, I am trying to write some data into excel sheets using c#. so far i have written the following code. But i am not able to proceed further ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.