Following is the code which will retrieve records from excel file and show up in output window. You can modify it to show up in grid as well. Myexcel file contains a sheet named cities and three columns Product, qty and price.
Imports System.Data.OleDb
Imports System.Data
Imports System.Diagnostics
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim m_sConn1 As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=E:\AutoExcel\myexcel.xls;" & _
"Extended Properties=""Excel 8.0;HDR=Yes"""
Dim conn2 As New OleDbConnection(m_sConn1)
Dim da As New OleDbDataAdapter("Select * From [Cities$]", conn2)
Dim ds As DataSet = New DataSet()
da.Fill(ds)
Debug.WriteLine(vbCrLf & "InventoryData:" & vbCrLf & "==============")
Dim dr As DataRow
For Each dr In ds.Tables(0).Rows 'Show results in output window
Debug.WriteLine(System.String.Format("{0,-15}{1, -6}{2}", _
dr("Product"), dr("Qty"), dr("Price")))
Next
conn2.Close()
End Sub
End Class
No comments:
Post a Comment