您的位置:首页 > 编程语言 > ASP

黑马程序员之ASP.NET学习笔记:将数据导出到Word或Excel

2012-11-19 22:17 363 查看
-------------------------------------2345王牌技术员联盟2345王牌技术员联盟、期待与您交流!-------------------------------



ASP.NET(C#)将数据导出到Word或Excel

命名空间:

using System.IO;

using System.Text;

将DataGrid的数据导出到Excel

string excelname="excel文件名";

HttpContext.Current.Response.Charset = "GB2312";

HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;

HttpContext.Current.Response.ContentType = "application/ms-excel";

HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment;filename=" + excelname +".xls");

dr1.Page.EnableViewState = false;

StringWriter sw = new StringWriter();

HtmlTextWriter tw = new HtmlTextWriter(sw);

dr1.RenderControl(tw);

HttpContext.Current.Response.Write(sw.ToString());

HttpContext.Current.Response.End();

将DataGrid的数据导出到Word

string excelname="word文件名";

HttpContext.Current.Response.Charset = "GB2312";

HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;

HttpContext.Current.Response.ContentType = "application/ms-winword";

HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment;filename=" + excelname +".doc");

dr1.Page.EnableViewState = false;

StringWriter sw = new StringWriter();

HtmlTextWriter tw = new HtmlTextWriter(sw);

dr1.RenderControl(tw);

HttpContext.Current.Response.Write(sw.ToString());

HttpContext.Current.Response.End();

---------------------------------------------------
2345王牌技术员联盟、2345王牌技术员联盟、期待与您交流!---------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: