码迷,mamicode.com
首页 > Windows程序 > 详细

C# GridView导出excel和word文件

时间:2016-06-25 17:50:53      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

//导出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文件

标签:

原文地址:http://www.cnblogs.com/zssblog/p/5616499.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!