471,337 Members | 1,170 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Use of Excel Interop within C#

I've been tasked to translate a C# program to VB. (Dot Net 2.0.)

I'm starting from scratch with no documentation and I'm trying to understand
what the original programmer did. (The 'original programmer' is conveniently
off on vacation so I can't ask him.) The program deals in some way with Excel
spreadsheets. There is a line of code in the program:

CurSheet =
(Microsoft.Office.Interop.Excel.Worksheet)WB.Works heets.get_Item(driverName);

where CurSheet is 'Microsoft.Office.Interop.Excel.Worksheet '.

What I don't understand is that get_Item call. It leads to something
labeled (in the VS tab) as 'Sheets [from metadata]' which in turn is
apparently a file within directory 'c:\temp\5232$Excel.dll$v1.1.4322. The
file name itself is Microsoft.Office.Interop.Excel.Sheets.cs.

I have no idea how or why this file was generated. Has anyone else out
there ever seen anything like this? What techniques are being used here?

Aug 25 '08 #1
5 1817
From the looks of it he's getting the current sheet off of the file in
that directory (something like the default sheet).
If he's not getting the default sheet then it looks like he's getting
a reference to a sheet. Unfortunately the Excel interop class was
really designed for VB so I coded all my excel processes into a VB.NET
dll and accessed it from C#. This means that the constructors have
like 15 elements and in VB they're optional.
Aug 25 '08 #2
B. Chernick used his keyboard to write :
I've been tasked to translate a C# program to VB. (Dot Net 2.0.)

I'm starting from scratch with no documentation and I'm trying to understand
what the original programmer did. (The 'original programmer' is conveniently
off on vacation so I can't ask him.) The program deals in some way with Excel
spreadsheets. There is a line of code in the program:

CurSheet =
(Microsoft.Office.Interop.Excel.Worksheet)WB.Works heets.get_Item(driverName);

where CurSheet is 'Microsoft.Office.Interop.Excel.Worksheet '.

What I don't understand is that get_Item call. It leads to something
labeled (in the VS tab) as 'Sheets [from metadata]' which in turn is
apparently a file within directory 'c:\temp\5232$Excel.dll$v1.1.4322. The
file name itself is Microsoft.Office.Interop.Excel.Sheets.cs.

I have no idea how or why this file was generated. Has anyone else out
there ever seen anything like this? What techniques are being used here?
Just looking at the words in the code (and having seen Excel) it seems
that a particular worksheet is selected from the entire workbook, based
on some "driverName". This selected worksheet is assinged to the
variable CurSheet.

I'm guessing that a "correcter" C# syntax would be
CurSheet =
(Microsoft.Office.Interop.Excel.Worksheet)WB.Works heets[driverName];

This (the [] instead of a call to Item) is regular C# to get a specific
item from some list. I just don't know if that still holds with
"interop" calls.

Hans Kesting
Aug 25 '08 #3
If all you're doing is reading data from excel (which will greatly simplify
your life), then abandon the interop.

google something like this

Excel LoadDataSet "select * from [Sheet1$]"

Or you can go here:
http://sholliday.spaces.live.com/blog/cns!A68482B9628A842A!176.entry
and find the code in there (somewhere). Code is downloadable.
If the developer is coming back, then wait for him to come back. That
excel interop crap is tricky.
Too tricky.

"B. Chernick" <BC*******@discussions.microsoft.comwrote in message
news:D1**********************************@microsof t.com...
I've been tasked to translate a C# program to VB. (Dot Net 2.0.)

I'm starting from scratch with no documentation and I'm trying to
understand
what the original programmer did. (The 'original programmer' is
conveniently
off on vacation so I can't ask him.) The program deals in some way with
Excel
spreadsheets. There is a line of code in the program:

CurSheet =
(Microsoft.Office.Interop.Excel.Worksheet)WB.Works heets.get_Item(driverName);

where CurSheet is 'Microsoft.Office.Interop.Excel.Worksheet '.

What I don't understand is that get_Item call. It leads to something
labeled (in the VS tab) as 'Sheets [from metadata]' which in turn is
apparently a file within directory 'c:\temp\5232$Excel.dll$v1.1.4322. The
file name itself is Microsoft.Office.Interop.Excel.Sheets.cs.

I have no idea how or why this file was generated. Has anyone else out
there ever seen anything like this? What techniques are being used here?

Aug 25 '08 #4
I'm not absolutely sure I understand your response. However I've found
something that might make things simpler. That file in the temp directory is
definitely 'temp'. It only appears when I do a 'Go To Definition' and
vanishes when I close the project. I've also checked the project properties
to make sure there aren't any exotic references or declarations. (Looks
fairly standard. As far as I can tell the C# and VB project properties are
identical.)

Given that, my only mystery (I think) is why the C# line

CurSheet =
(Microsoft.Office.Interop.Excel.Worksheet)WB.Works heets.get_Item(driverName);

does not want to translate into VB, given that both projects have the same
reference to the interop and both use the same declared variables?

Still trying. I get the feeling I'm missing something simple.

"cfps.Christian" wrote:
From the looks of it he's getting the current sheet off of the file in
that directory (something like the default sheet).
If he's not getting the default sheet then it looks like he's getting
a reference to a sheet. Unfortunately the Excel interop class was
really designed for VB so I coded all my excel processes into a VB.NET
dll and accessed it from C#. This means that the constructors have
like 15 elements and in VB they're optional.
Aug 25 '08 #5
That's probably the solution. When I changed the C# to WorkSheet[driverName]
and ran it through the translator, I got this:

CurSheet = DirectCast(WB.Worksheets.Item(driverName),
Microsoft.Office.Interop.Excel.Worksheet)

No apparent errors.

(The programmer has admited that the original code is not a finished product.)

Thanks.

"Hans Kesting" wrote:
B. Chernick used his keyboard to write :
I've been tasked to translate a C# program to VB. (Dot Net 2.0.)

I'm starting from scratch with no documentation and I'm trying to understand
what the original programmer did. (The 'original programmer' is conveniently
off on vacation so I can't ask him.) The program deals in some way with Excel
spreadsheets. There is a line of code in the program:

CurSheet =
(Microsoft.Office.Interop.Excel.Worksheet)WB.Works heets.get_Item(driverName);

where CurSheet is 'Microsoft.Office.Interop.Excel.Worksheet '.

What I don't understand is that get_Item call. It leads to something
labeled (in the VS tab) as 'Sheets [from metadata]' which in turn is
apparently a file within directory 'c:\temp\5232$Excel.dll$v1.1.4322. The
file name itself is Microsoft.Office.Interop.Excel.Sheets.cs.

I have no idea how or why this file was generated. Has anyone else out
there ever seen anything like this? What techniques are being used here?

Just looking at the words in the code (and having seen Excel) it seems
that a particular worksheet is selected from the entire workbook, based
on some "driverName". This selected worksheet is assinged to the
variable CurSheet.

I'm guessing that a "correcter" C# syntax would be
CurSheet =
(Microsoft.Office.Interop.Excel.Worksheet)WB.Works heets[driverName];

This (the [] instead of a call to Item) is regular C# to get a specific
item from some list. I just don't know if that still holds with
"interop" calls.

Hans Kesting
Aug 25 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Mansi | last post: by
1 post views Thread by Bernd Muent | last post: by
5 posts views Thread by Mike in Santa Rosa | last post: by
5 posts views Thread by =?Utf-8?B?U3R1YXJ0?= | last post: by

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.