-
在ASP.NET中访问Excel电子表格
在ASP.NET中访问Excel电子表格
by Alex Lowe(From:www.aspfree.com) 翻译:eheart
下面提供在ASP.NET中如何查询Excel电子表格的源代码:
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.ADO" %>
<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
Dim myDataset As New DataSet()
'当然,你可以通过ODBC来实现
Dim strConn As String="Provider=Microsoft.Jet.OLEDB.4.0;"&"Data Source=C:\exceltest.xls;"& "Extended Properties=’Excel 8.0;’"
'你必须在你指定的电子表格名后加上符号$
Dim myADODataSetCommand As New ADODataSetCommand("SELECT * FROM [Sheet1$]", strConn)
myADODataSetCommand.TableMappings.Add("Table", "ExcelTest")
myADODataSetCommand.FillDataSet(myDataset)
DataGrid1.DataSource = myDataset.Tables(0).DefaultView
DataGrid1.DataBind()
End Sub
</script>
<p>
<asp:Label id=Label1 runat="server">
电子表格内容:</asp:Label>
</p>
<asp:DataGrid id=DataGrid1 runat="server"/>