qq_38081404
IT界98新星
2018-09-25 03:19
采纳率: 70.6%
浏览 541

asp.net gridview 导出Excel 分页???

  • 这是我现在用的方法

    /// 导出方法
        /// </summary>
        /// <param name="GridView"></param>
        /// <param name="filename">保存的文件名称</param>
        private void ExportGridViewForUTF8(GridView GridView, string filename)
        {
    
            string attachment = "attachment; filename=" + filename;
    
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", attachment);
    
            Response.Charset = "UTF-8";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            Response.ContentType = "application/ms-excel";
            System.IO.StringWriter sw = new System.IO.StringWriter();
    
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            GridView.RenderControl(htw);
    
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
        }
    

    图片说明

  • 这是我用这个方法实现的效果

  • 现在我想让他把后面分页的5224条数据,全显示在一个Excel表里

  • 应该怎么写啊 大佬们
    *

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • weixin_42501524
    发个福福 2018-09-25 07:26
    已采纳
    如果你不想去掉网页的分页  可以向下面 加上前两句代码  重新从数据源查一遍数据 ,gridview 的属性allowpaging 设为false  不会影响网页分页
    
    
        private void ExportGridViewForUTF8(GridView GridView1, string filename)
    {
    
        GridView1.AllowPaging = false;
    
        bind();//重新为gridview 绑定数据
    
    
        string attachment = "attachment; filename=" + filename;
    
        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", attachment);
    
        Response.Charset = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
        Response.ContentType = "application/ms-excel";
        System.IO.StringWriter sw = new System.IO.StringWriter();
    
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView1.RenderControl(htw);
    
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
    }
    
    点赞 评论
  • caozhy

    有很多方法,但是就你现有代码来说,最省事的办法,就是打开一个页面,不要分页地加载全部的数据,再导出。

    点赞 评论

相关推荐