Solved

How would you do a connection string in OLE DB and write a select * from Excel spreadsheet query?

Posted on 2006-06-22
2
228 Views
Last Modified: 2010-04-23
I figured out how to connect to the Excel spreadsheet in my code, but the only thing holding me back is how to write a query string in vb.net with OLE DB.  How would I do a simple select * from (excel spreadsheet) and how would I do a select column1 from (excel spreadsheet)?

0
Comment
Question by:VBBRett
2 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16961941
Select * From [Sheet1$]
Select Column1 From [Sheet1$]

Bob
0
 
LVL 35

Accepted Solution

by:
YZlat earned 500 total points
ID: 16962338
here is a function that thakes two parameters (1) path to your excel file, 2) is optional - Spreadsheet name. If not specified, uses "Sheet1" by default)reads data from Excel into a DataTable:

Function ReadDataFromExcel(ByVal path As String, Optional ByVal sheet As String = "Sheet1") As System.Data.DataTable
        Dim ds As New DataSet
        Dim da As OleDbDataAdapter
        Dim conn As OleDbConnection
        Try

            conn = New OleDbConnection( _
                  "provider=Microsoft.Jet.OLEDB.4.0; " & _
                  "data source=" & path & "; " & _
                  "Extended Properties=Excel 8.0;")

            da = New OleDbDataAdapter("SELECT * FROM [" & sheet & "$]", conn)

            conn.Open()

            da.Fill(ds)
            ''get the name of the last column
            Dim lastCol As DataColumn
            lastCol = ds.Tables(0).Columns(ds.Tables(0).Columns.Count - 1)
            ''remove the last column from a DataTable
            ds.Tables(0).Columns.Remove(lastCol)

        Catch ex As Exception
            Console.Write(ex.Message)
        Finally
            If conn.State = ConnectionState.Open Then
                conn.Close()
            End If
        End Try
        ReadDataFromExcel = ds.Tables(0)
    End Function
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question