Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Pro Visual Basic 2005
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 13th, 2006, 03:26 PM
Registered User
 
Join Date: Apr 2006
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

 
Old November 22nd, 2006, 10:09 AM
Authorized User
 
Join Date: Nov 2006
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ef1196
Default

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"";"
    .Open()
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
    da.Fill(ds)
    '* The line below assumes you have a DataGridView named dg
    '* placed on your form.
    dg.DataSource = ds.Tables(0)
End With

cn.close()

Good Luck!
Earl Francis
 
Old June 26th, 2007, 09:04 AM
Registered User
 
Join Date: Apr 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
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/

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






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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.