using System.Drawing;
using System.IO;
using System.Text;
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();
}
/// <summary>
/// 此方法必重写,否则会出错
/// </summary>
/// <param name="control"></param>
public override void VerifyRenderingInServerForm(Control control)
{
}
导出Excel
Export("application/ms-excel", "Employee information.xls");
导出Word
Export("application/ms-word", "员工信息.doc");
本文介绍了一种使用ASP.NET进行文件导出的方法,包括如何将GridView中的数据导出为Excel和Word文件。通过设置HTTP响应头和使用HtmlTextWriter渲染控件,实现了不同文件类型的导出。
194

被折叠的 条评论
为什么被折叠?



