Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Pro Visual Basic 2005
Password Reminder
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Pro Visual Basic 2005 For advanced Visual Basic coders working in version 2005. Beginning-level questions will be redirected to other forums, including Beginning VB 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro Visual Basic 2005 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 13th, 2006, 03:26 PM
Registered User
Join Date: Apr 2006
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Select xls worksheet thru VS2005

Hi all,
I can open, read, write and save excel file thru VS2005, unfortunately I can't select which worksheet to read/write. It always default to the first worksheet of excel file. Any advices are appreciated.

Best regards,
Khanh Nguyen

  #2 (permalink)  
Old November 22nd, 2006, 10:09 AM
Authorized User
Join Date: Nov 2006
Location: , , USA.
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ef1196

Here is one method. Others will have a different take I'm sure.

'* Create your Connection Object
Dim cn As New System.Data.OleDb.OleDbConnection
With cn
    '* Notice the double quotes ("") before and after the Extended Properties
    '* portion of the connection string.
    '* This connection string attempts to open C:\Book1.xls - replace
    '* with your full path file name.
    .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
        & "C:\Book1.xls;Extended Properties=""Excel 8.0;HDR=YES"";"
End With

'* Create the Command Object
Dim cmd As New System.Data.OleDb.OleDbCommand
With cmd
    .Connection = cn
    .CommandType = CommandType.Text
    '* The worksheet that you want to open is enclosed in brackets
    '* and ends with a $ sign.
    .CommandText = "SELECT * FROM [Sheet1$]"
    Dim da As New System.Data.OleDb.OleDbDataAdapter(cmd)
    Dim ds As New DataSet
    '* The line below assumes you have a DataGridView named dg
    '* placed on your form.
    dg.DataSource = ds.Tables(0)
End With


Good Luck!
Earl Francis
  #3 (permalink)  
Old June 26th, 2007, 09:04 AM
Registered User
Join Date: Apr 2006
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts

This is my code to open/read/write and save excel file, but unable to select the worksheet (it does not mattter ws index or ws name was entered), only select the first sheet. Please help, any advices are appreciated.

        Dim xlApp As Microsoft.Office.Interop.Excel.Application
        Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
        Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
        Dim bOpen As Boolean

      xlApp = CType(CreateObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)
        xlBook = CType(xlApp.Workbooks.Open(sSpec_Path), Microsoft.Office.Interop.Excel.Workbook)
        xlApp.DisplayAlerts = False : bOpen = True
        If bOpen Then
            xlSheet = CType(xlBook.Worksheets(1), Microsoft.Office.Interop.Excel.Worksheet) 'Unable to select any worksheet event 2, 3 was entered or ws name
            With xlSheet.Application
            ...... in here i can read/write/

           xlBook.Close() : xlApp.DisplayAlerts = True : xlApp.Quit()


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the different between VS2005 and VS.NET? zach007 Visual Studio 2005 4 September 11th, 2007 12:44 PM
Can I still use VS2005 Beta? Aaron Edwards ASP.NET 2.0 Basics 0 September 13th, 2006 07:34 PM
Can I still use VS2005 Beta? Aaron Edwards ASP.NET 2.0 Basics 1 December 12th, 2005 11:57 AM
.mpp to .xls sachin-csharp .NET Framework 2.0 0 November 1st, 2004 12:25 AM

All times are GMT -4. The time now is 07:58 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.