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, "没有数据可以导出!");
}
}
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。