首页 > 编程 > .NET > 正文

在asp.net中调用Excel 文件

2024-07-10 12:58:28
字体:
来源:转载
供稿:网友
vb.net源代码
<%@ 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()

''我们可以使用excel odbc driver
dim strconn as string = "provider=microsoft.jet.oledb.4.0;" & _
"data source=c:/exceltest.xls;" & _
"extended properties=""excel 8.0;"""

''我们必须使用$在sheet后面
dim mydata as new oledbdataadapter("select * from [sheet1$]", strconn)
mydata.tablemappings.add("table", "exceltest")
mydata.fill(mydataset)

datagrid1.datasource = mydataset.tables(0).defaultview
datagrid1.databind()
end sub
</script>

<html>
<head></head>
<body>
<p><asp:label id=label1 runat="server">spreadsheet contents:</asp:label></p>
<asp:datagrid id=datagrid1 runat="server"/>/
</body>
</html>

c# syntax

<%@ page language="c#" %>
<%@ import namespace="system.data.oledb" %>
<%@ import namespace="system.data" %>
<%@ import namespace="system" %>


<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;";

oledbdataadapter mycommand = new oledbdataadapter("select * from [sheet1$]", strconn);

dataset mydataset = new dataset();
mycommand.fill(mydataset, "excelinfo");
datagrid1.datasource = mydataset.tables["excelinfo"].defaultview;
datagrid1.databind();
}
</script>
<html>
<head></head>
<body>
<p><asp:label id=label1 runat="server">spreadsheet contents:</asp:label></p>
<asp:datagrid id=datagrid1 runat="server"/>/
</body>
</html>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表