在ASP.NET访问Excel文件 (VB and C#)

2008-02-22 09:32:57来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

C# Code
---------------------------------------------
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.ADO" %>
<script language="C#" runat="server">
protected void Page_Load(Object Src, EventArgs E)
{
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;"
"Data Source=C:\\exceltest.xls;"
"Extended Properties=Excel 8.0;";
ADODataSetCommand myCommand = new ADODataSetCommand("SELECT * FROM [Sheet1$]", strConn);
DataSet myDataSet = new DataSet();
myCommand.FillDataSet(myDataSet, "ExcelInfo");
DataGrid1.DataSource = myDataSet.Tables["ExcelInfo"].DefaultView;
DataGrid1.DataBind();
}
</script>
<p><asp:Label id=Label1 runat="server">Excel表格内容:</asp:Label></p>
<asp:DataGrid id=DataGrid1 runat="server"/>

VB.NET Code
----------------------------------------------
<%@ 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()
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">Excel表格内容:</asp:Label></p>
<asp:DataGrid id=DataGrid1 runat="server"/>

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:[DataGird]如何截取过长的字符串

下一篇:通过系统配置来提高ASP.NET应用程序的稳定性