zoukankan      html  css  js  c++  java
  • C# GridView 导出Excel表

    出错1:类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内
    解决方案:在后台文件中重载VerifyRenderingInServerForm方法,如:
    public override void VerifyRenderingInServerForm(Control control)
    {
    }

    出错2:只能在执行 Render() 的过程中调用 RegisterForEventValidation(RegisterForEventValidation can only be called during Render();

    解决方案:在源中,添加红色部分<%@ Page Language="C#" EnableEventValidation = "false"CodeFile="ExportGridView.aspx.cs" Inherits="ExportGridView" %>

    另外,在使用时,把GRIDVIEW获取数据返回DataSet类型数据,或者返回SqlDataReader类型数据也可以。

    调用事件里,实例化一个DATASET, 让它值等于DS

             private void GridViewBind()
            {
                string sortExpression = this.grid.Attributes["SortExpression"] == null ? "Code" : this.grid.Attributes["SortExpression"];
                bool isASCDirection = true;
                if (grid.Attributes["SortDirection"] == "DESC")
                {
                    isASCDirection = false;
                }
                int totalCounts = (int)SqlHelper.GetCountNumber("WS_User", "ID", ltConditon.Text);
                AspNetPager1.RecordCount = totalCounts;
                grid.DataSource = SqlHelper.GetPagedData("WS_User", ltConditon.Text, sortExpression, isASCDirection, AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex);
                grid.DataBind();
            }
         private void GridBindAll()
            {
                string sql = "SELECT * FROM WS_User";
                SqlDataReader dr = SqlHelper.ExecuteDataReader(sql, CommandType.Text);
                grid.DataSource = dr;
                grid.DataBind();
            }
            protected void btnExcel_Click(object sender, EventArgs e)
            {
                grid.AllowPaging = false;
                grid.AllowSorting = false;
                grid.Columns[7].Visible = false;
                GridBindAll();
                DateTime dt = DateTime.Now;
                string str = dt.ToString("yyyyMMddhhmmss");
                str = str + ".xls";
                GridViewToExcel(grid, "application/ms-excel", str);
    
                grid.AllowPaging = true;
                grid.AllowSorting = true;
                grid.Columns[2].Visible = true;
                GridViewBind();
                // Export(gvRecord, "application/ms-excel", str);
            }
            /// <summary>
            /// 将网格数据导出到Excel
            /// </summary>
            /// <param name="ctrl">网格名称(如GridView1)</param>
            /// <param name="FileType">要导出的文件类型(Excel:application/ms-excel)</param>
            /// <param name="FileName">要保存的文件名</param>
            public static void GridViewToExcel(Control ctrl, string FileType, string FileName)
            {
                HttpContext.Current.Response.Charset = "GB2312";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;//注意编码
                HttpContext.Current.Response.AppendHeader("Content-Disposition",
                    "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
                HttpContext.Current.Response.ContentType = FileType;//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
                ctrl.Page.EnableViewState = false;
                StringWriter tw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(tw);
                ctrl.RenderControl(hw);
                HttpContext.Current.Response.Write("<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />" + tw.ToString());
                HttpContext.Current.Response.End();
    
            }
            public override void VerifyRenderingInServerForm(Control control)
            {
                //base.VerifyRenderingInServerForm(control);
            }

     注意:只是导出了在GridView中的列,没有把数据库中的其它没有展示的列都导出。

  • 相关阅读:
    C语言指向指针的指针
    C语言注意事项
    C语言指针
    C语言字符串
    C语言数组
    C语言交换两个数的值
    C语言位运算符
    C语言各种进制输出
    C语言中各种进制的表示
    C 语言sizeof运算符
  • 原文地址:https://www.cnblogs.com/BrokenIce/p/5836416.html
Copyright © 2011-2022 走看看