一、读取并在GridView中显示
Try
Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source='" & Server.MapPath("a.xls") _
& "';" & "Extended Properties=Excel 8.0;"
Dim objConn As New OleDbConnection(sConnectionString)
objConn.Open()
Dim objCmdSelect As New OleDbCommand("SELECT * FROM [myexcel$]", objConn)
Dim objAdapter1 As New OleDbDataAdapter()
objAdapter1.SelectCommand = objCmdSelect
Dim objDataset1 As New DataSet()
objAdapter1.Fill(objDataset1, "myexcel")
GridView1.DataSource = objDataset1.Tables(0).DefaultView
GridView1.DataBind()
objConn.Close()
Catch ex As OleDbException
Response.Write(ex)
End Try
二、读取指定行列中的值
Dim i, j As Integer
Try
Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source='" & Server.MapPath("a.xls") _
& "';" & "Extended Properties=Excel 8.0;"
Dim objConn As New OleDbConnection(sConnectionString)
objConn.Open()
Dim objCmdSelect As New OleDbCommand("SELECT * FROM [myexcel$]", objConn)
Dim objAdapter1 As New OleDbDataAdapter()
objAdapter1.SelectCommand = objCmdSelect
Dim objDataset1 As New DataSet()
objAdapter1.Fill(objDataset1, "myexcel")
Dim dt As DataTable = objDataset1.Tables(0)
For i = 0 To dt.Rows.Count - 1
Response.Write(dt.Rows(i)("顺序号") & " " & dt.Rows(i)("货号") & "<br>")
Next
objConn.Close()
Catch ex As OleDbException
Response.Write(ex)
End Try