zoukankan      html  css  js  c++  java
  • 转帖:[Asp.net技术]asp.net中Word\Excel的导出数据制作(懒人发掘)

    这是asp.net 中将页面数据导出word、excel的常用代码(以datagrid为例)

    导出word代码:

    Response.Clear();
       Response.AddHeader("content-disposition", "attachment;filename=FileName.doc");

       Response.Charset = "utf-8";

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

       DataGrid1.RenderControl(htmlWrite);

       Response.Write(stringWrite.ToString());

       Response.End();

    导出excel代码:

    Response.Clear();
       Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");

       Response.Charset = "utf-8";

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

       DataGrid1.RenderControl(htmlWrite);

       Response.Write(stringWrite.ToString());

       Response.End();

    这个代码很容易可以在网上搜到,但是如果页面的datagrid有排序和分页时就会出现错误,这是时候如果用循环导出数据,实在是不爽。现有一个新的办法:

    第一种:

    复制同样一个datagrid,赋同样的数据,隐藏,取消分页和排序功能:

    Datagrid2.Visible=true;
       Response.Clear();
       Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");

       Response.Charset = "utf-8";

       Response.Cache.SetCacheability(HttpCacheability.NoCache);

       Response.ContentType = "application/vnd.xls";
       this.EnableViewState=false;
       System.IO.StringWriter stringWrite = new System.IO.StringWriter();

       System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

       Datagrid2.RenderControl(htmlWrite);

       Response.Write(stringWrite.ToString());

       Response.End();
       Datagrid2.Visible=false;

    即可倒出漂亮的excel文件,word同上

    同理可得:导出前取消分页和排序,导出再恢复,可以实现这个功能

  • 相关阅读:
    typro常用快捷键
    02: kali-linux破解密码运行脚本并隐藏进程
    01:kali安装使用
    01: 模拟挖矿黑客攻击过程
    12: docker-compose部署django+nginx+uwsgi+celery+redis+mysql
    11: Django + gunicorn + Nginx 的生产环境部署
    博客说明
    计算机中原码,反码,补码之间的关系
    修改linux下yum镜像源为国内镜像
    webp图片技术调研最终结论(完全真实数据可自行分析)
  • 原文地址:https://www.cnblogs.com/whitewin/p/290155.html
Copyright © 2011-2022 走看看