前台代码:Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
后台代码:Default.aspx.cs<!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>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
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;
using System.Data.OleDb;
public partial class _Default : System.Web.UI.Page
{
private static string connstr=ConfigurationManager.AppSettings["ConnectionString"].ToString();
private static string dbpath = ConfigurationManager.AppSettings["DBPATH"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GridView1.DataSource = CreateDataSource();
this.GridView1.DataBind();
}
}
private DataSet CreateDataSource()
{
String ExcelDBPath = connstr + Server.MapPath(dbpath) + ";";
OleDbConnection conn = new OleDbConnection(ExcelDBPath);
OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", conn);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet);
return myDataSet;
}
}
web.configusing System.Data;
using System.Configuration;
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;
using System.Data.OleDb;
public partial class _Default : System.Web.UI.Page
{
private static string connstr=ConfigurationManager.AppSettings["ConnectionString"].ToString();
private static string dbpath = ConfigurationManager.AppSettings["DBPATH"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GridView1.DataSource = CreateDataSource();
this.GridView1.DataBind();
}
}
private DataSet CreateDataSource()
{
String ExcelDBPath = connstr + Server.MapPath(dbpath) + ";";
OleDbConnection conn = new OleDbConnection(ExcelDBPath);
OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", conn);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet);
return myDataSet;
}
}
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings>
<add key="ConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0; Extended Properties=Excel 8.0;Data Source="/>
<add key="DBPATH" value="ExcelDB\username.xls"></add>
</appSettings>
<system.web>
<compilation debug="true"/></system.web></configuration>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings>
<add key="ConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0; Extended Properties=Excel 8.0;Data Source="/>
<add key="DBPATH" value="ExcelDB\username.xls"></add>
</appSettings>
<system.web>
<compilation debug="true"/></system.web></configuration>