往前走,一片诱人的新绿尽收眼底,那是满池的新荷。荷叶有的平展着圆盘浮在水面上;有的绿伞般在空中摇曳;有的兜着水珠把阳光反射得灿烂夺目;有的长得非常高,却未展开叶面,勇敢地挺立着。荷花则多半含苞待放,白中透粉的一朵朵花儿,活像一个个花仙子借着微风,在池中裙袂飞扬,翩然起舞。
本文实例讲述了asp.net实现Gradview绑定数据库数据并导出Excel的方法。分享给大家供大家参考,具体如下:
protected void showData_Click(object sender, EventArgs e)
{
SqlConnection myConnection
= new SqlConnection("Data Source=localhost;Initial Catalog=test;User ID=sa;password=sa");
SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM booklist", myConnection);
DataSet ds = new DataSet();
ad.Fill(ds);
this.gvShowData.DataSource = ds;
this.gvShowData.DataBind();
}
//导出Excel表
protected void btnExportToExcel_Click(object sender, EventArgs e)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AddHeader("Content-Type", "application/vnd.ms-excel");
Response.AddHeader("Content-Disposition", "myexcelfile.xls");
//以此编码模式导出才不会出现乱码
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvShowData.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
//一定要写,否则出错!!
public override void VerifyRenderingInServerForm(Control control)
{
}
希望本文所述对大家asp.net程序设计有所帮助。
以上就是asp.net实现Gradview绑定数据库数据并导出Excel的方法。不好放下你的。当梦想没有了以后,你还能够生存,但是你虽生犹死。更多关于asp.net实现Gradview绑定数据库数据并导出Excel的方法请关注haodaima.com其它相关文章!