GridView 分页模式下导出所有记录至Excel

来源:互联网 发布:spss软件免费中文版 编辑:程序博客网 时间:2024/04/28 06:54
if (GV_qkcj.Rows.Count > 0)
            {
                GV_qkcj.AllowPaging = false;
                BindQK();
                foreach (GridViewRow dg in this.GV_qkcj.Rows)
                {
                    dg.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat: @;");
                    dg.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat: @;");
                } 
                Response.Clear();
                Response.ClearContent();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now.ToString("_yyyyMMdd_HHmmss") + ".xls");
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                Response.ContentType = "application/ms-excel";
                StringWriter sw = new StringWriter();
                HtmlTextWriter htw = new HtmlTextWriter(sw);
                GV_qkcj.RenderControl(htw);
                Response.Write(sw.ToString());
                Response.Flush();
                Response.End();
                GV_qkcj.AllowPaging = true;
                BindQK();
            }
            else
            {
                Response.Write("没有数据");
            }
0 0
原创粉丝点击