綁定數據到DataGrid
{
try
{
string strSql="select * from house";
SqlDataAdapter da=new SqlDataAdapter(strSql,conn);
DataSet ds=new DataSet();
da.Fill(ds,"TableName");
this.dgRole.DataSource=ds;
this.dgRole.DataBind();
}
catch(Exception Err)
{
Response.Write(Err.Message);
}
}
通過按鈕事件將數據插入EXCEL
this.MapPath("/Project/UserUpload/Role.xls")+";Extended Properties=Excel 8.0;");
OleDbCommand cmd = cn.CreateCommand();
cmd.CommandText = "CREATE TABLE Role (列名1 VARCHAR(10),列名2 VARCHAR(20))";
cn.Open();
cmd.ExecuteNonQuery();
for(int i=0;i<this.dgRole.Items.Count;i++)
{
cmd.CommandText = "INSERT INTO [Role$] VALUES('"+
this.dgRole.Items[i].Cells[0].Text+"','"+
this.dgRole.Items[i].Cells[1].Text+"')";
cmd.ExecuteNonQuery();
}
cn.Close();