您的位置:首页 > 其它

将Excel中数据读入到GridView中

2009-04-28 18:25 288 查看
1、页面代码:

<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default2.aspx.cs"Inherits="Default2"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title></title>
</head>
<body>
<formid="form1"runat="server">
<div>
</div>
<asp:PanelID="Panel1"runat="server"BorderWidth="3px">
<asp:GridViewID="GridView1"runat="server"
AllowPaging="True"onpageindexchanging="GridView1_PageIndexChanging"
Width="702px">
</asp:GridView>
</asp:Panel>
<p>
<asp:ButtonID="Button1"runat="server"Text="读取数据"onclick="Button1_Click"
style="height:26px"/>
</p>
</form>
</body>
</html>


.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}

2、执行代码

usingSystem;
usingSystem.Web.UI.WebControls;
usingSystem.Data;
usingSystem.Data.OleDb;
publicpartialclassDefault2:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
}
protectedvoidButton1_Click(objectsender,EventArgse)
{
GridView1.DataSource=CreateDataSource();
GridView1.DataBind();
}
privateDataSetCreateDataSource()
{
stringconnString;
connString="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="+Server.MapPath("Checklist.xls")+";ExtendedProperties=Excel8.0;";
OleDbConnectionolecon=newOleDbConnection(connString);
OleDbDataAdaptermyda=newOleDbDataAdapter("SELECT*FROM[Sheet1$]",connString);
DataSetmyds=newDataSet();
myda.Fill(myds);
returnmyds;
}
protectedvoidGridView1_PageIndexChanging(objectsender,GridViewPageEventArgse)
{
GridView1.PageIndex=e.NewPageIndex;
GridView1.DataSource=CreateDataSource();
GridView1.DataBind();
}
}



.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: