zoukankan      html  css  js  c++  java
  • .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;

  • 相关阅读:
    CSS——制作天天生鲜主页
    HTML——制作一个图片列表
    HTML——制作一个简易菜单栏
    CSS——三种页面引入方法
    【20170903】模拟赛
    【LA 3942】 Remember the word
    【BZOJ 1036】 树的统计count
    UVA 12299 RMQ with shifts
    【20170706】次短路
    【20170706】保卫萝卜
  • 原文地址:https://www.cnblogs.com/jlcoder/p/3522641.html
Copyright © 2011-2022 走看看