Dim myOleDbConnection As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=c:/test.xls;" & _ "Extended Properties=""Excel 8.0;""") Dim myOleDbCommand As OleDbCommand = New OleDbCommand("SELECT * FROM [Sheet1$]",myOleDbConnection) '如果你想读出Sheet2的内容 ,把Sheet1$改成Sheet2$即可 Dim myData As OledbDataAdapter= New OledbDataAdapter(myOleDbCommand)
Dim myDataset As New DataSet() myData.Fill(myDataset) '完成从OledbDataAdapter对象到DataSet的转换 DataGrid1.DataSource = myDataset.Tables(0).DefaultView DataGrid1.DataBind() '完成数据帮定,显示数据
四. 具体的源代码:
<%@ Page Language="VB" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.Oledb" %> <script language="VB" runat="server"> Sub Page_Load(sender As Object, e As EventArgs) Dim myDataset As New DataSet() Dim myOleDbConnection As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=c:/2.xls;" & _ "Extended Properties=""Excel 8.0;""") Dim myOleDbCommand As OleDbCommand = New OleDbCommand("SELECT * FROM [Sheet1$]",myOleDbConnection) Dim myData As OledbDataAdapter= New OledbDataAdapter(myOleDbCommand)
myData.Fill(myDataset)
DataGrid1.DataSource = myDataset.Tables(0).DefaultView DataGrid1.DataBind() End Sub </script>