Results 1 to 4 of 4

Thread: [RESOLVED] OleDB/Excel Sheet Import

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Resolved [RESOLVED] OleDB/Excel Sheet Import

    Hey all,
    I currently have some code to import a spread into a dataset but it is dependant on the name of the sheet ie sheet1$. I would like this import to work on the first sheet of a xls file no matter what the sheet name is. Any ideas?

    vb Code:
    1. Private Function GetAllRows(ByRef objCon As OleDb.OleDbConnection) As DataSet
    2.         Dim results As New DataSet("ExcelRows")
    3.         Dim com As New OleDb.OleDbCommand("select * from [sheet1$]", objCon)
    4.         Dim da As New OleDb.OleDbDataAdapter()
    5.         da.SelectCommand = com
    6.         da.Fill(results)
    7.         Return results
    8.     End Function

  2. #2
    Addicted Member
    Join Date
    Mar 2007
    Posts
    163

    Re: OleDB/Excel Sheet Import

    Hi,

    Have you tried getting the worksheets from the excel workbook object? If you know the name of the excel file you can get the workbook object and then from there all the sheets that are included in the workbook and use the first one in your case.

    Cheers,

    --Stav.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: OleDB/Excel Sheet Import

    Thanks,
    I actually did get it working via the Excel Object but I think I would like to stick to OleDB if it is possible.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: OleDB/Excel Sheet Import

    Got it!

    vb Code:
    1. Public Function GetSheetName(ByVal fileName As String) As String
    2.         Dim objConn As OleDb.OleDbConnection
    3.         Dim dt As System.Data.DataTable = Nothing
    4.  
    5.         Try
    6.             objConn = Connect()
    7.             dt = objConn.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, Nothing)
    8.             Close(objConn)
    9.             Return dt.Rows(0).Item("TABLE_NAME").ToString
    10.         Catch ex As Exception
    11.             Return Nothing
    12.         Finally
    13.             ' Clean up.
    14.             If Not dt Is Nothing Then
    15.                 dt.Dispose()
    16.             End If
    17.         End Try
    18.  
    19.     End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width