当我试图获得excel导出时,Button_click什么都不做ASP.NET C#

时间:2016-05-11 11:27:18

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

当我试图从我的mysqldatabase中获取excel导出时,它在button_click上没有做任何事情。我可以看到gridview正在填充DataTable但是在回发时它没有下载我的gridview的excel输出。我无法找出问题出在哪里。谢谢你的帮助。

这是我的.aspx gridview代码

private void BindGrid()
        {

            DataTable dt = book.getAllbookExcelExport();
            GridView1.DataSource = dt;
            GridView1.DataBind();

        }

        protected void btnExport_ServerClick(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.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;
                this.BindGrid();

                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();

            }

        }

这是我的数据绑定和按钮点击

protected void Page_Load(object sender, EventArgs e)
        {
            Page.Form.Attributes.Add("enctype", "multipart/form-data");

            ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
            scriptManager.RegisterPostBackControl(btnSave);

            if (!Page.IsPostBack)
            {

                this.BindGrid();

            }
        }

这是我的page_load

Verifica

0 个答案:

没有答案