关闭

把Excel文件中的数据读入到DataGrid中

标签: datagridexceldatasetaspserverproperties
433人阅读 评论(0) 收藏 举报
分类:

完整代码如下:
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>

<script runat="server">

private DataSet CreateDataSource(){
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=C://Inetpub//wwwroot//contacts.xls;"+
"Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [ContactList$]", strConn);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet);
return myDataSet;
}


public void Page_Load(Object sender, EventArgs e){
if (!IsPostBack) {
mygrid.DataSource = CreateDataSource();
mygrid.DataBind();
}

}


</script>


<center>
<form runat="server">
<asp:datagrid runat="server" AutoGenerateColumns="false"
width="500" id="mygrid">

<HeaderStyle BorderColor="White" BackColor="black"
ForeColor="White"
Font-Bold="True"
Font-Name="Arial"
Font-Size="9" HorizontalAlign="Center"/>

<ItemStyle   BorderColor=""
BackColor="#FFFFF0"
ForeColor="Black"
Font-Name="Arial"
Font-Size="8"
Font-Bold="False" HorizontalAlign="Center"/>

<Columns>

<asp:BoundColumn HeaderText="姓名" ReadOnly="true" DataField="姓名"/>
<asp:BoundColumn HeaderText="性别" ReadOnly="true" DataField="性别"/>
<asp:BoundColumn HeaderText="Email" ReadOnly="true" DataField="地址"/>
</Columns>

</asp:datagrid>
</form>

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:79581次
    • 积分:1011
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:81篇
    • 译文:0篇
    • 评论:8条
    最新评论