【问题标题】:Export from GridView to Excel save popup not working从 GridView 导出到 Excel 保存弹出窗口不起作用
【发布时间】:2013-12-02 19:15:59
【问题描述】:

我想将 GridView 中的数据导出到 ASP.NET 网站上的 excel 文件中。我仅为此目的添加了一个 GridView

<body>
   <form id="mainForm" runat="server">
      <asp:GridView ID="exportGrid" runat="server">
      </asp:GridView>
   </form>
....
</body>

在代码隐藏中我有这个:

public override void VerifyRenderingInServerForm(Control control) { }

var result = GetDataIQueryable(); //A method that returns an IQueryable
exportGrid.DataSource = result; 
exportGrid.DataBind();
Response.Clear();
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=" + "ExcelFile.xls");
Response.ContentType = "application/excel";
var sw = new System.IO.StringWriter();
var htw = new HtmlTextWriter(sw);
exportGrid.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();

问题是没有出现允许我在我的计算机上保存 excel 文件的弹出窗口。我应该在我的代码中更改什么?

【问题讨论】:

    标签: asp.net gridview export-to-excel


    【解决方案1】:

    在您的按钮点击事件中使用此代码

     protected void btnExport_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=FormReport.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        using (StringWriter sw = new StringWriter())
        {
            HtmlTextWriter hw = new HtmlTextWriter(sw);
    
            //To Export all pages
            GridView1.AllowPaging = false;            
            BindGridView();
    
            GridView1.HeaderRow.BackColor = Color.White;
            foreach (TableCell cell in GridView1.HeaderRow.Cells)
            {
                cell.BackColor = GridView1.HeaderStyle.BackColor;
            }
            foreach (GridViewRow row in GridView1.Rows)
            {
                row.BackColor = Color.White;
                foreach (TableCell cell in row.Cells)
                {
                    if (row.RowIndex % 2 == 0)
                    {
                        cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
                    }
                    else
                    {
                        cell.BackColor = GridView1.RowStyle.BackColor;
                    }
                    cell.CssClass = "textmode";
                }
            }
    
            GridView1.RenderControl(hw);
    
            //style to format numbers to string
            string style = @"<style> .textmode { } </style>";
            Response.Write(style);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
            GridView1.Dispose();
        }
    }
    #endregion
    
    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }
    

    并在 .aspx 页面上使用

     <%@ Page Title="" Language="C#" EnableEventValidation="false"%>
    

    【讨论】:

    • 如果该解决方案对您有用,请不要忘记接受它作为答案
    • 同样的问题。我没有收到任何错误,但没有出现弹出窗口,所以我无法保存 Excel 文件。
    • 您是否在导出按钮的点击事件中使用该代码。
    • 是的在点击事件方法中。我在您的代码(stackoverflow.com/questions/2041482/…)中发现了这个问题,更正后的 Response.End() 仍然无法正常工作。
    • 是,设置为 false。
    【解决方案2】:

    我知道这有点太晚了,但我发布这个只是为了提供信息。我在 cmets 中找到了答案,但我仍然想用代码提交答案,因为我自己发现很难找到代码,所以答案是如果 您的网格在更新面板内。您需要为按钮添加回发触发器。

      private void RegisterPostBackControl()
        {
            ScriptManager.GetCurrent(this).RegisterPostBackControl(yourButton);
        }
    
    
        public override void VerifyRenderingInServerForm(Control control)
        {
            /* Verifies that the control is rendered */
        }
    

    您的按钮点击事件:

    protected void yourButton_Click(object sender, EventArgs e)
        {
    
            ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "full", true);
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=FormReport.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            using (StringWriter sw = new StringWriter())
            {
                HtmlTextWriter hw = new HtmlTextWriter(sw);
    
                //To Export all pages
                yourGrid.AllowPaging = false;
                var emps = FunctionThatGetsGridValues();
                yourGrid.DataSource = emps;
                yourGrid.DataBind();
    
                yourGrid.HeaderRow.BackColor = Color.White;
                foreach (TableCell cell in yourGrid.HeaderRow.Cells)
                {
                    cell.BackColor = yourGrid.HeaderStyle.BackColor;
                }
                foreach (GridViewRow row in yourGrid.Rows)
                {
                    row.BackColor = Color.White;
                    foreach (TableCell cell in row.Cells)
                    {
                        if (row.RowIndex % 2 == 0)
                        {
                            cell.BackColor = yourGrid.AlternatingRowStyle.BackColor;
                        }
                        else
                        {
                            cell.BackColor = yourGrid.RowStyle.BackColor;
                        }
                        cell.CssClass = "textmode";
                    }
                }
    
                yourGrid.RenderControl(hw);
    
                //style to format numbers to string
                string style = @"<style> .textmode { } </style>";
                Response.Write(style);
                Response.Output.Write(sw.ToString());
                Response.Flush();
                Response.End();
                yourGrid.Dispose();
            }
        }
    

    并且在页面加载时不要忘记添加:

      protected void Page_Load(object sender, EventArgs e)
        {
            this.RegisterPostBackControl();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      • 2013-03-25
      • 2012-08-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多