Nisha prince
Nisha prince

Reputation: 88

while clicking on export button i want to export aspx gridview?

I want to export ASPxGridView while clicking on a button as export.

I am using code below but not getting the needed results:

protected void btn_excel_Click1(object sender, EventArgs e)
    {
        ASPxGridViewExporter1 . WriteXlsToResponse();
    }

Upvotes: 0

Views: 707

Answers (3)

Amal Ps
Amal Ps

Reputation: 713

Try this

  1. Session["Excel"] - The items that you want to export (In my case I'm storing this in a session).

    using (var exporter = new NpoiExport()) {

        DataTable dt = new DataTable();
        dt = (DataTable)Session["Excel"];
        exporter.ExportDataTableToWorkbook(dt, "Result");
    
        string saveAsFileName = string.Format("Results-{0:d}.xls", DateTime.Now);
    
    
        Response.ContentType = "application/excel";
    
        Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", saveAsFileName));
    
        Response.Clear();
    
        Response.BinaryWrite(exporter.GetBytes());
    
        Response.End();
    }
    

Upvotes: 1

Akhil Singh
Akhil Singh

Reputation: 730

Export to Excel Use this code.

<asp:GridView ID="GrdAccess" runat="server" AutoGenerateColumns="false" Width="70%" AllowPaging="true" PageSize="20" CssClass="Grid" RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000" PagerStyle-CssClass="pgr" OnPageIndexChanging="GrdAccess_PageIndexChanging"></asp:GridView>

protected void ExportToExcel(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
                GrdAccess.AllowPaging = false;
                this.getProjectDetails();

                GrdAccess.HeaderRow.BackColor = Color.White;
                foreach (TableCell cell in GrdAccess.HeaderRow.Cells)
                {
                    cell.BackColor = GrdAccess.HeaderStyle.BackColor;
                }
                foreach (GridViewRow row in GrdAccess.Rows)
                {
                    row.BackColor = Color.White;
                    foreach (TableCell cell in row.Cells)
                    {
                        if (row.RowIndex % 2 == 0)
                        {
                            cell.BackColor = GrdAccess.AlternatingRowStyle.BackColor;
                        }
                        else
                        {
                            cell.BackColor = GrdAccess.RowStyle.BackColor;
                        }
                        cell.CssClass = "textmode";
                    }
                }

                GrdAccess.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();
            }
        }


    public override void VerifyRenderingInServerForm(Control control)
        {

        }

Upvotes: 0

sukumar2267
sukumar2267

Reputation: 130

if you want export the grid view data to excel then do with data table. bind the gridview data into data table and export the data table data to excel.

Upvotes: 0

Related Questions