用gridview显示来自excel表格里的数据
2006-08-21 17:14:11 来源:WEB开发网核心提示:在asp.net 2.0中,如何用gridview显示来自建立好的Excel文件的内容呢,用gridview显示来自excel表格里的数据,其实是很简单的,下面给出简单代码片断PRotected void Page_Load(object sender, EventArgs e){ OleDbConnection D
在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的内容取出来。
更多精彩
赞助商链接