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文件的主要内容,如果未能解决你的问题,请参考以下文章

c#导出excel慢

C#实现GridView导出Excel

用C#实现WORD文档导出

asp.net中,如何实现将page页面各个空间的属性值,导出下载到一个word或excel中

C#导出excel 不安装office excel情况下

将 GridView 导出到 Excel 2007