郑德才博客 记录学习,记录工作,学习知识分享!

导出gridwiew中的数据

protected void Button1_Click(object sender, EventArgs e)
{
ExportToExcel();
}
public void ExportToExcel()
{
if (GridView1.Rows.Count > 0)
{
string FileName = "导出文件名";
GridView1.Caption = FileName;
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode("表名", System.Text.Encoding.UTF8) + "\".xls");
Response.Charset = "gb2312";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
else
{
MessageBox.Show(Page, "没有数据可以导出!");
}
}
2012年3月30日 | 发布:郑德才博客 | 分类:工作之路 | 评论:0

发表留言: