Use of Excel Interop within C#

  • Thread starter Thread starter B. Chernick
  • Start date Start date
B

B. Chernick

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.Worksheets.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?
 
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.Worksheets.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.Worksheets[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
 
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.
 
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.Worksheets.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.
 
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 said:
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.Worksheets.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.Worksheets[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top