PC Review


Reply
Thread Tools Rate Thread

CSharp and Excel

 
 
gumbystation@gmail.com
Guest
Posts: n/a
 
      30th Dec 2006
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.

 
Reply With Quote
 
 
 
 
RobinS
Guest
Posts: n/a
 
      30th Dec 2006
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).WorkSheets(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.
--------------------------
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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.
>



 
Reply With Quote
 
Frank Rizzo
Guest
Posts: n/a
 
      30th Dec 2006
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


(E-Mail Removed) 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.
>

 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      30th Dec 2006
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> oXL.Sheets("Sheet1").Activate(); //Doesn't compile


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


 
Reply With Quote
 
Luke Smith
Guest
Posts: n/a
 
      30th Dec 2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>
> (E-Mail Removed) 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.
>>


 
Reply With Quote
 
gumbystation@gmail.com
Guest
Posts: n/a
 
      30th Dec 2006
@ 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@lukesmithDOTnet> wrote:
> 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.com> wrote in messagenews:(E-Mail Removed)...
>
>
>
> > 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 -


 
Reply With Quote
 
Laurent Bugnion [ASP.NET MVP]
Guest
Posts: n/a
 
      1st Jan 2007
Hi,

(E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel Solver functionality within Csharp application . . akantrowitz@hotmail.com Microsoft C# .NET 0 2nd Mar 2006 12:02 AM
CSharp VB Excel COM Differences Alan Roberts Microsoft C# .NET 7 6th Jan 2006 03:28 PM
Excel communicating to csharp application. roger.camargo@gmail.com Microsoft Excel Discussion 1 17th Nov 2005 05:51 PM
Excel communicating to csharp application roger.camargo@gmail.com Microsoft Excel Discussion 0 17th Nov 2005 03:41 PM
csharp / excel interop / lock / stack overflow Grzegorz Kaczor Microsoft C# .NET 1 10th Jun 2005 11:09 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:01 AM.