asp.net中将Excel文件(.xls)绑定到DataGrid!,asp.net中,sp.net中将,p.net中将E,.net中将Ex,net中将Exc,et中将Exce,t中将Excel,中将Excel文,将Excel文件,Excel文件.,xcel文件.x,cel文件.xl,el文件.xls,l文件.xls绑,文件.xls绑定,件.xls绑定到,.xls绑定到D,xls绑定到Da,ls绑定到Dat,s绑定到Data,绑定到DataG,定到DataGr,到DataGri,DataGrid

本站地址:http://www.bajiao123.com

asp.net中将Excel文件(.xls)绑定到DataGrid!
首先,在*.aspx.cs文件头部添加如下引用:
using System.Data.OleDb;//用于将Excel文件绑定到DataGrid
其次,在Page_Load()函数中添如下示例代码:
if(!IsPostBack)
{
string strCon="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("../xls_bang/bang.xls")+";Extended Properties=´Excel 8.0;HDR=YES;IMEX=1´";
OleDbConnection oleCon=new OleDbConnection(strCon);
OleDbDataAdapter oleDA=new OleDbDataAdapter("select * from [Sheet1$]",oleCon);
DataSet ds=new DataSet();
oleDA.Fill(ds);
dgBang.DataSource=ds;
dgBang.DataBind();
}
说明:bang.xls是需要绑到DataGrid(dgBang)的Excel文件。
Sheet1是bang.xls中的一个工作表单(work sheet)
"HDR=Yes;" :说明第一行包含的是列名,而不是数据
"IMEX=1;" :告诉驱动总是读交叉数据列作为文本
("HDR=Yes;" indicates that the first row contains columnnames, not data"IMEX=1;" tells the driver to always read "intermixed" data columns as text)
本站地址:http://www.bajiao123.com

