|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Opening a Sheet from Excel in VB.NET?
On the code below everything works great to populate a datagrid with excel sheet "Query1" but is there a way to find all the sheet names and then choose from a list rather then having a set name? If my sheet is named "Sheet1" for instance the code below will not work. What I would like to do is be able to populate a list with sheet names choose from the list then open up that sheet.
Any Ideas. PHP Code:
__________________
George - www.ipdg3.com Helping Developers and Programmers Find Resources Forums - Contests - Tutorials - Source Code ORB - Wireless Site - Online Gear |
|
#2
|
|||
|
|||
|
There may be other ways to do it but this is what I've always used.
I haven't tested, it's just to get you started: Code:
dim xlApp as Excel.Application, xlWs as Excel.WorkSheet
set xlApp = new Excel.Application
with xlApp
with .WorkBooks.Open(stExcelFile)
for each xlWs as .WorkSheets
'add xlWs.name to list box
next xlWs
xlWs.close
end with
call xlApp.quit
end with
You might want to play around with checking if there's a running instance of excel you can use instead of creating a new instance etc. |
|
#3
|
|||
|
|||
|
Hello epl
Thanks for the reply I will try it out. I think that is somewhat similiar to VB6 code as well isn't it? I really do appretiate your reply. This will allow me to have the user pick a WorkSheet then open it in the datagrid control. Thanks |
|
#4
|
|||
|
|||
|
epl
I tried out your code in .NET and it is not quite working. You have to use the COM object right "Microsoft Excel 9.0 Oject Library". The "for each xlWs as .WorkSheets" Does not seem to be right. Any help would be great and if I can do this without COM References that would be even better. PHP Code:
|
|
#5
|
|||
|
|||
|
Not, it's definitely wrong - I'm sorry about that.
It should be IN and not AS. I think I probably copied the as from the dim statement at the top. Like I said, I didn't test but just typed it straight in. You're right about referencing Excel 9.0 library etc. That's what I was referring to as the way I've always done it. I'm sure you could pick up / write something to parse the sheet names from the xls file itself but i don't know how / whether it would be worth your while. Maybe try searching for details of the excel file format on msdn. |
|
#6
|
|||
|
|||
|
Here is what I used and it worked like a champ. You will however have to use the COM Reference. "Microsoft Excel 9.0 Oject Library"
This example allows you to pass in a location and a file name as "stExcelFile" and the populates the ComboBox1 with Sheet Names. PHP Code:
|
|
#7
|
|||
|
|||
|
Hello,
I would like to know how are you passing the sheet name to the SQL string after you get it from the combobox Thanks |
|
#8
|
|||
|
|||
|
Hello
Yeah I'm trying to do a similar thing, opening an excel worksheet and feeding some data into it. Is there no .net way of doing this rather than using the COM components, seems a bit old fashioned! |
|
#9
|
|||
|
|||
|
Well one bad thing I have found since talking with a lot of .NET programmers is since VS.NET did not come with a lot of controls and reference that were used in VB6.You now have two options buy a 3rd party control that does what you want and supports .NET or use COM. I have not found any problems with using COM so far but it would be nice if they ported a lot of the stuff up to .NET. I am carious if Office 2003 will allow you to use a new version of "Microsoft Excel Oject Library"
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > Opening a Sheet from Excel in VB.NET? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|