Solved

Reading Excel Using OLEDB when column header contain spaces and special characters

Posted on 2010-08-31
4
1,840 Views
Last Modified: 2013-11-27
I am using the following answer to a previous question.  I am using Visual Basic 2010.
Read Data from Excel using OLEDB
http://vb.net-informations.com/excel-2007/vb.net_excel_oledb.htm
=============
My problem is that I don't have control over the Excel sheet, and the column headers contain spaces and other non-alphanumeric characters.  I need to be able to refer to individual columns (fields), but I need help with the syntax, OR find a way to ignore the Excel column headers and be able to refer to the fields with the generic F1, F2, etc (which is what happens if row 1 is empty).

Typical column headers as an example:  "A: Address"  "B: Name"

And this will fail, but I don't know the right syntax to make it work...
("select 'A: Address' from [Sheet1$]", MyConnection)

Similarly, this will fail...
("select * from [Sheet1$] where 'B: Name' like 'Fred%'", MyConnection)



Imports System.Data
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim MyConnection As System.Data.OleDb.OleDbConnection
            Dim DtSet As System.Data.DataSet
            Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
            MyConnection = New System.Data.OleDb.OleDbConnection _
            ("provider=Microsoft.Jet.OLEDB.4.0;"  _
            " Data Source='c:\testfile.xls'; " _
             "Extended Properties=Excel 8.0;")
            MyCommand = New System.Data.OleDb.OleDbDataAdapter _
                ("select * from [Sheet1$]", MyConnection)
            MyCommand.TableMappings.Add("Table", "TestTable")
            DtSet = New System.Data.DataSet
            MyCommand.Fill(DtSet)
            DataGridView1.DataSource = DtSet.Tables(0)
            MyConnection.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class

Open in new window

0
Comment
Question by:motokent
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 33566376
If I am not mistaken it is not single-quoted, but square-bracketed.

("select * from [Sheet1$] where [B: Name] like 'Fred%'", MyConnection)
0
 

Author Comment

by:motokent
ID: 33566453
That's it!  Thank you for the quick response.  Before I close it... can you tell me if there's a way to tell it to ignore the field names (basically assume that row 1 contains data rather than column header)?
0
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 500 total points
ID: 33566600
Have a look  http://www.connectionstrings.com/excel

The connection string should contain HDR=No;
e.g.  Data Source='c:\testfile.xls';HDR=No;
0
 

Author Closing Comment

by:motokent
ID: 33566797
Excellent.  This helped a lot.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

730 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