• .NET DataGrid 导出Excel 无分页


     #region  导出Excel
        //
        protected void BtnExcelClick(object sender, EventArgs e)
        {
    
            ToExcel();
    
        }
    
    
        public void ToExcel()
        {
    
            Response.ContentType = "application/vnd.ms-excel";
            Response.Charset = "";
            Response.AddHeader("Content-Disposition",
                                        "attachment; filename=" + HttpUtility.UrlEncode("企业规模统计", Encoding.UTF8).ToString() +
                                        ".xls");
            this.EnableViewState = false;
            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
            int nCur = dgList.CurrentPageIndex;
            int nSize = dgList.PageSize;
            dgList.AllowPaging = false;
            dgList.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(dgExport_ItemDataBound);
            dgList.CurrentPageIndex = 0;
            BindData();
    
            dgList.RenderControl(hw);
    
            //以下恢复分页
            dgList.AllowPaging = true;
            dgList.CurrentPageIndex = nCur;
            dgList.PageSize = nSize;
            BindData();
            string temp = sw.ToString().Replace("<br/>", "<br style='mso-data-placement:same-cell;'/> ");
            temp = temp.Replace("border="0"", "border="1"");
    
            Response.Write(temp);
            Response.End();
    
        }
        protected static void dgExport_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                foreach (TableCell cell in e.Item.Cells)
                {
                    if (Regex.IsMatch(cell.Text.Trim(), @"^d{12,}$") || Regex.IsMatch(cell.Text.Trim(), @"^d+[-]d+$"))
                    {
                        cell.Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                    }
                }
            }
        }
        #endregion

     需要引用的命名空间:using System.Text.RegularExpressions;

  • 相关阅读:
    C#学习教程
    数据库
    读写信号量
    qt配置tensorflow + opencv 提示protoc版本错误
    【1】EIGEN-Matrix类
    c++11的新特性
    ubuntu 16.04 python+tensorflow安装路径查看
    python的常用数据类型及其使用
    windows文件转LINUX文件格式
    ubuntu 16.04 + GPU 1080 + NVIDIA384
  • 原文地址:https://www.cnblogs.com/jlcoder/p/3522641.html
Copyright © 2020-2023  润新知