« ASP操作Excel技术总结 csdn文档iis6基本设置 »

Asp.net访问Excel数据源

=================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表,引入名命空间不一样就是了。

原创文章如转载,请注明:转载自悠悠博客 [ http://www.ajaxstu.com/ ]

相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。