在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的內容取出來。