Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 17th, 2003, 04:38 PM
iamtgo3 iamtgo3 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2001
Location: USA
Posts: 312 iamtgo3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 27 m 43 sec
Reputation Power: 7
Send a message via ICQ to iamtgo3 Send a message via AIM to iamtgo3 Send a message via MSN to iamtgo3 Send a message via Yahoo to iamtgo3
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:
 strConn "Provider=Microsoft.Jet.OLEDB.4.0;" _
                      
"Data Source=" stExcelFile ";" _
                      
"Extended Properties=Excel 8.0;"
            
'Excel Sheet Name as TableName
            myCommand = New OleDbDataAdapter("SELECT * FROM [Query1$]", strConn)
            myCommand.Fill(myDataset, "Query1")
            DataGrid1.DataSource = myDataset.Tables(0).DefaultView 
__________________
George - www.ipdg3.com
Helping Developers and Programmers Find Resources
Forums - Contests - Tutorials - Source Code
ORB - Wireless Site - Online Gear

Reply With Quote
  #2  
Old April 20th, 2003, 07:25 AM
epl epl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: Dublin
Posts: 413 epl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 18 m 18 sec
Reputation Power: 8
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.

Reply With Quote
  #3  
Old April 20th, 2003, 12:19 PM
iamtgo3 iamtgo3 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2001
Location: USA
Posts: 312 iamtgo3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 27 m 43 sec
Reputation Power: 7
Send a message via ICQ to iamtgo3 Send a message via AIM to iamtgo3 Send a message via MSN to iamtgo3 Send a message via Yahoo to iamtgo3
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

Reply With Quote
  #4  
Old April 21st, 2003, 02:04 PM
iamtgo3 iamtgo3 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2001
Location: USA
Posts: 312 iamtgo3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 27 m 43 sec
Reputation Power: 7
Send a message via ICQ to iamtgo3 Send a message via AIM to iamtgo3 Send a message via MSN to iamtgo3 Send a message via Yahoo to iamtgo3
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:
Public Sub getSheets(ByVal stExcelFile As String)

        
Dim xlApp As Excel.Application
        Dim xlWs 
As Excel.Worksheet

        xlApp 
= New Excel.Application()

        
With xlApp
            With 
.Workbooks.Open(stExcelFile)
                for 
each xlWs as .WorkSheets 'The "AS" is underlined in blue here you sure this is write.
                    ComboBox1.Items.Add(xlWs.Name)     '
add xlWs.name to list box
                Next xlWs
                xlWs
.close() 'This is underlined in blue
            End With
            Call xlApp.Quit()
        End With

    End Sub 

Reply With Quote
  #5  
Old April 21st, 2003, 02:10 PM
epl epl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: Dublin
Posts: 413 epl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 18 m 18 sec
Reputation Power: 8
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.

Reply With Quote
  #6  
Old April 22nd, 2003, 11:15 AM
iamtgo3 iamtgo3 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2001
Location: USA
Posts: 312 iamtgo3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 27 m 43 sec
Reputation Power: 7
Send a message via ICQ to iamtgo3 Send a message via AIM to iamtgo3 Send a message via MSN to iamtgo3 Send a message via Yahoo to iamtgo3
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:
Public Sub getSheets(ByVal stExcelFile As String)
        
Dim xlApp As Excel.Application
        Dim xlWs 
As Excel.Worksheet

        xlApp 
= New Excel.Application()

        
With xlApp
            With 
.Workbooks.Open(stExcelFile)
                For 
Each xlWs In .Worksheets
                    ComboBox1
.Items.Add(xlWs.Name)     'add xlWs.name to list box
                Next xlWs
            End With
            Call xlApp.Quit()
        End With

    End Sub 

Reply With Quote
  #7  
Old December 1st, 2003, 10:46 PM
PhillyQueenD PhillyQueenD is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Philadelphia, PA
Posts: 1 PhillyQueenD User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to PhillyQueenD
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

Reply With Quote
  #8  
Old December 3rd, 2003, 05:05 AM
bennf bennf is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 1 bennf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Talking

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!

Reply With Quote
  #9  
Old December 3rd, 2003, 07:34 AM
iamtgo3 iamtgo3 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2001
Location: USA
Posts: 312 iamtgo3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 27 m 43 sec
Reputation Power: 7
Send a message via ICQ to iamtgo3 Send a message via AIM to iamtgo3 Send a message via MSN to iamtgo3 Send a message via Yahoo to iamtgo3
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"

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > Opening a Sheet from Excel in VB.NET?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT