=================connectExcel.aspx.cs中源码这样写下来==============================
using System;
using System.Data;
using System.Data.Odbc;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class connectExcel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String connstr = @"Driver=Microsoft Excel Driver (*.xls);Dbq=D:\hooyes\wwwroot\excelsample.xls;";
OdbcConnection myConnection = new OdbcConnection(connstr);
myConnection.Open();
OdbcCommand myCommand = new OdbcCommand("select * from[Sheet1$]", myConnection);
MyDataGrid.DataSource = myCommand.ExecuteReader();
MyDataGrid.DataBind();
myConnection.Close();
}
}
然后新建一张 connectExcel.aspx===================================
代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="connectExcel.aspx.cs" Inherits="connectExcel" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>西狐 连接EXCEL数据源</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataGrid ID="MyDataGrid" runat=server></asp:DataGrid>
</div>
</form>
</body>
</html>
========================
再建一个张Excel工作表,就OK了。本例保存EXCEL在D:\hooyes\wwwroot\excelsample.xls
:ohh: 西狐扫盲贴 HOHO:ohh:
这是用ODBC.net 还可以用OLEDB.net 访问 Excel表,引入名命空间不一样就是了。
Asp.net访问Excel数据源
原创文章如转载,请注明:转载自悠悠博客 [ http://www.ajaxstu.com/ ]
相关文章:
- 同一个用户不允许同时登陆两次(2007-11-15 1:17:43)
- 罗列全部session和application(2007-11-12 6:26:16)
- 访问和更新Cookies集合(2007-11-11 1:42:29)
- asp中cookie使用示例(2007-11-7 3:2:28)
- asp重定向-response.redirect和server.transfer(2007-10-24 9:18:30)
- vbscript Replace 函数(2007-10-21 4:33:2)
- asp:Server对象(2007-10-11 7:9:58)
- vbscript:日期格式常数(2007-10-10 7:30:49)
- ASP动态包含文件的改进方法(2007-10-8 1:5:48)
- For...Next 语句(2007-10-4 9:43:2)
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
