Rajkumar R

Rajkumar R

  • NA
  • 183
  • 220.7k

How to read and view Excel file in vb.net

Dec 14 2011 6:20 AM

If (Radiobuttonlist1.SelectedItem.Value = 2) Then

Dim path As String

            'path = upload.PostedFile.FileName

            path = "D:\Karthi Scripts\XML CHK\shg_repayment.xls"

            'Response.Write(path)

            'Response.End()

            Dim connect As String

            connect = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source= '" + path + "'" + "Extended Properties='Excel 4.0;HDR=Yes;IMEX=1;'"

            Dim conn As New OleDbConnection(connect)

            conn.Open()

            Dim ds = New DataSet()

            Dim od As New OleDbCommand("SELECT * FROM [Sheet1$]", conn)

            Dim orr As OleDbDataReader

            grdView.DataSource = orr

            grdView.DataBind()

            conn.Close()

        End If

The Above code i have used to read the excel file and view in the gridview.But i m getting the following error
Format of the initialization string does not conform to the OLE DB specification.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException: Format of the initialization string does not conform to the OLE DB specification.

Source Error:

Line 341:            connect = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source= '" + path + "'" + "Extended Properties='Excel 4.0;HDR=Yes;IMEX=1;'" Line 342:            Dim conn As New OleDbConnection(connect) Line 343:            conn.Open() Line 344:            Dim ds = New DataSet() Line 345:            Dim od As New OleDbCommand("SELECT * FROM [Sheet1$]", conn)
Post solution for this 
Thanks in advance

Answers (8)