首先,在*.aspx.cs文件頭部添加如下引用:
using system.data.oledb;//用于將excel文件綁定到datagrid
其次,在page_load()函數(shù)中添如下示例代碼:
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;" :說明第一行包含的是列名,而不是數(shù)據(jù)
"imex=1;" :告訴驅(qū)動總是讀交叉數(shù)據(jù)列作為文本
("hdr=yes;" indicates that the first row contains columnnames, not data"imex=1;" tells the driver to always read "intermixed" data columns as text)
新聞熱點
疑難解答
圖片精選