在asp.net 2.0中,如何用gridview显示来自建立好的excel文件的内容呢,其实是很简单的,下面给出简单代码片断
protected void page_load(object sender, eventargs e)
{
oledbconnection dbconnection = new
oledbconnection("provider=microsoft.jet.oledb.4.0;" + "data source=" +
server.mappath("~/app_data/demo1.xls") + ";" + "extended
properties=/"excel 8.0;hdr=yes/"");
dbconnection.open();
string sqlstring = "select * from [sheet1$]";
oledbcommand dbcommand = new oledbcommand(sqlstring, dbconnection);
idatareader dbreader = dbcommand.executereader();
gridview1.datasource = dbreader;
gridview1.databind();
dbreader.close();
dbconnection.close();
}
其中,把demo1.xls放在app_data目录下,这里用select * from [sheet1$]来将sheet1的内容取出来。
新闻热点
疑难解答