Click here to Skip to main content
15,888,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a GridView and when I click Export Button to export my data into CSV. I click the button and it takes all the data correctly but it doesn't show anything, no popup to ask me to open or to save .

Here is my code

 protected void btnExport_Click(object sender, EventArgs e)
{
    ExportCSV();
}

protected void ExportCSV()
{
    GridViewSW.DataSource = ViewState["source"];
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=Orders.csv");
    Response.Charset = "";
    Response.ContentType = "application/text";
    GridViewSW.AllowPaging = false;        
    GridViewSW.DataBind();

    StringBuilder columnbind = new StringBuilder();
    for (int k = 0; k < GridViewSW.Columns.Count; k++)
    {
        columnbind.Append(GridViewSW.Columns[k].HeaderText + ",");
    }
    columnbind.Append("\r\n");

    for (int i = 0; i < GridViewSW.Rows.Count; i++)
    {
        for (int j = 0; j < GridViewSW.Columns.Count; j++)
        {
            columnbind.Append(GridViewSW.Rows[i].Cells[j].Text + ",");
        }
        columnbind.Append("\r\n");
    }



    Response.Output.Write(columnbind.ToString());
    Response.Flush();
    Response.End();

}
Posted
Comments
ZurdoDev 14-Aug-15 22:16pm    
Try Response.Write instead of Response.Output

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900