DataGrid导出到Word/Excel文档

将DataGrid数据导出到Word文档

private void ExportToWord_Click(object sender, System.EventArgs e)
{
 Response.Clear();
 Response.AddHeader("content-disposition", "attachment;filename=FileName.doc");
 Response.Charset = "";
 Response.Cache.SetCacheability(HttpCacheability.NoCache);
 Response.ContentType = "application/vnd.word";
 System.IO.StringWriter stringWrite = new System.IO.StringWriter();
 System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
 dgDevice.RenderControl(htmlWrite);
 Response.Write(stringWrite.ToString());
 Response.End();
}

将DataGrid数据导出到Excel文档

private void ExportToExcel_Click(object sender, System.EventArgs e)
{
 Response.Clear();
 Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
 Response.Charset = "";
 Response.Cache.SetCacheability(HttpCacheability.NoCache);
 Response.ContentType = "application/vnd.xls";
 System.IO.StringWriter stringWrite = new System.IO.StringWriter();
 System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
 dgDevice.RenderControl(htmlWrite);
 Response.Write(stringWrite.ToString());
 Response.End();
}

本文为codeproject节选,原文地址为:http://www.codeproject.com/aspnet/DAtaGridExportToExcel.asp

http://skylaugh.cnblogs.com/archive/2006/05/25/409110.html

阅读更多
上一篇在ASP.NET里得到网站的域名
下一篇ASP.NET中常用的文件上传下载方法
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭