1
Response.Clear();
2
Response.Buffer
=
true
;
3
Response.Charset
=
"
GB2312
"
;
4
Response.AppendHeader(
"
Content-Disposition
"
,
"
attachment;filename=jtkc.xls
"
);
5
Response.ContentEncoding
=
System.Text.Encoding.GetEncoding(
"
GB2312
"
);
//
设置输出流为简体中文
6
Response.ContentType
=
"
application/ms-excel
"
;
//
设置输出文件类型为excel文件。
7
this
.EnableViewState
=
false
;
8
System.Globalization.CultureInfo myCItrad
=
new
System.Globalization.CultureInfo(
"
ZH-CN
"
,
true
);
9
System.IO.StringWriter oStringWriter
=
new
System.IO.StringWriter(myCItrad);
10
System.Web.UI.HtmlTextWriter oHtmlTextWriter
=
new
System.Web.UI.HtmlTextWriter(oStringWriter);
11
grvExcel.RenderControl(oHtmlTextWriter);
12
Response.Write(oStringWriter.ToString());
13
Response.End();
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
2
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
3
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
4
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
5
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
6
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
7
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
8
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
9
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
10
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
11
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
12
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
13
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
1.这过程中遇到一些问题,刚开始是提示GridView必须放在runat="server"的窗体中,但是已经设置了runat="server"但还是报错,问题解决如下:
只需要重写VerifyRenderingInServerForm方法,将内容清空就可以了。
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
public
override
void
VerifyRenderingInServerForm(Control control)
{
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
{
}
2。第二个问题是提示:只能在 Render(); 期间呼叫 RegisterForEventValidation
这个问题比较好解决,只需要在前台页面的@Page指令中加上一个属性:EnableEventValidation="false"
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-566599/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/12639172/viewspace-566599/