首页 热点资讯 义务教育 高等教育 出国留学 考研考公

asp.net(C#) 导出到word excel [100分]

发布网友

我来回答

2个回答

热心网友

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();

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com