| Author: anujj 03 Nov 2009 | Member Level: Bronze | Rating:  Points: 2 |
the following is my code i have collected so far to display xls/xlsx in a datagrid. WHAT SHOULD I DO TO PREVIEW THE EXCEL FILE CONTENTS IN A TEXTBOX?
CODE: Dim ds As New DataSet Dim result As DialogResult = OpenFileDialog1.ShowDialog() Try Dim wbPath As String If result = DialogResult.OK Then
Dim objFi As New FileInfo(OpenFileDialog1.FileName)
wbPath = OpenFileDialog1.FileName
'Dim connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & wbPath & ";" & "Extended Properties=Excel 12.0"
Dim sConnectionString As String = "provider=microsoft.ACE.oledb.12.0;" _ & "data source=" & wbPath _ & ";" & "extended properties=excel 12.0;"
ds.Clear()
Dim strSQL As String = "SELECT * FROM [Sheet1$]" Dim excelConnection As OleDbConnection = New OleDbConnection(sConnectionString) excelConnection.Open()
Dim dbCommand As OleDbCommand = New OleDbCommand(strSQL, excelConnection) Dim dataAdapter As OleDbDataAdapter = New OleDbDataAdapter(dbCommand)
dataAdapter.Fill(ds, "dTable") DataGridView1.DataSource = ds.Tables("dTable").DefaultView
' disposing used objects ds.Dispose() dataAdapter.Dispose() dbCommand.Dispose() excelConnection.Close() excelConnection.Dispose() End If Catch ex As Exception MessageBox.Show(ex.ToString()) End Try
|