C# GridView导出excel和word文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# GridView导出excel和word文件相关的知识,希望对你有一定的参考价值。
//导出excel按钮事件
protected void Button1_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "农田环境数据.xls");
}
//导出word按钮事件
protected void Button2_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "农田环境数据.doc");
//Export("application/ms-word", "农田环境数据.doc");//都可以
}
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
htmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw); Response.Write(tw.ToString());
Response.End();
}
// 此方法必重写,否则会出错
public override void VerifyRenderingInServerForm(Control control)
{
}
以上是关于C# GridView导出excel和word文件的主要内容,如果未能解决你的问题,请参考以下文章