Question : Read Data from Excel using OLEDB in Visual Basic 2010

I need to read contents of an Excel file into my VB application's datagrid control.  I'm using a solution from a previous question:
=================
= (Dhaest):Here is another example, surely written in vb.net (the version of .net has no impact)
= Read Data from Excel using OLEDB
= http://vb.net-informations.com/excel-2007/vb.net_excel_oledb.htm
=================
It works great, except for one thing.  I don't have control over the actual Excel file and it happens to have a space at the beginning of the sheet name (example:  " Vlan - 32" vs "Vlan -32").  The program chokes because of the space.  Here's the error message:
System.Data.OleDb.OleDbException (0x80040E14): Invalid bracketing of name 'Vlan - 32$'.

Any ideas?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
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 [ Vlan - 32$]", 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

Answer : Read Data from Excel using OLEDB in Visual Basic 2010

If you tried exactly what i put in, I noticed I didn't put the single quote right (you might have caught).
Should be:
' Vlan - 32$'


Random Solutions  
 
programming4us programming4us