不思议迷宫永恒王座,家庭园艺,中国工艺品信息网
//由gridviw导出为excel
public static void toexcel(system.web.ui.control ctl)
{
httpcontext.current.response.appendheader("content-disposition", "attachment;filename=excel.xls");
httpcontext.current.response.charset = "utf-8";
httpcontext.current.response.contentencoding = system.text.encoding.default;
httpcontext.current.response.contenttype = "application/ms-excel";//image/jpeg;text/html;image/gif;vnd.ms-excel/msword
ctl.page.enableviewstate = false;
system.io.stringwriter tw = new system.io.stringwriter();
system.web.ui.htmltextwriter hw = new system.web.ui.htmltextwriter(tw);
ctl.rendercontrol(hw);
httpcontext.current.response.write(tw.tostring());
httpcontext.current.response.end();
}
这是一段网上很多人使用的导出excel代码,但使用过程中发现很多不足的地方,其不用引入其它控件。
缺点:
1、我采用aspnetpager分页时只能导出第一页。
2、使用十分不灵活,对样式的控制,字段的控制不灵活。
使用中需要注意:
1、 <%@ page language="c#" autoeventwireup="true" enableeventvalidation="false" codefile="default.aspx.cs" inherits="_default"%>
加上这句。
2、在后台文件中加上:
public override void verifyrenderinginserverform(control control)
{
}
3、需引入 using system.io; 其它相关文章:
asp.net操作excel备忘录 asp.net连接excel的代码 在asp.net用c#建立动态excel
asp.net 操作excel的实现代码
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!!
点击进行留言回复
网友评论