Export gridview data into CSV file

后端 未结 3 1385
花落未央
花落未央 2020-12-08 17:22

I have a gridview control in ASP.Net 2.0 and i need to export this gridview data into CSV file.

I have bind this gridview with the dataset.After binding the dataset

相关标签:
3条回答
  • 2020-12-08 17:27

    Try following code, i used it already many times. It will export the data directly from gridview to csv file specified in the code.

    protected void btnExportCSV_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition",
         "attachment;filename=GridViewExport.csv");
        Response.Charset = "";
        Response.ContentType = "application/text";
    
        GridView1.AllowPaging = false;
        GridView1.DataBind();
    
        StringBuilder sb = new StringBuilder();
        for (int k = 0; k < GridView1.Columns.Count; k++)
        {
            //add separator
            sb.Append(GridView1.Columns[k].HeaderText + ',');
        }
        //append new line
        sb.Append("\r\n");
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            for (int k = 0; k < GridView1.Columns.Count; k++)
            {
                //add separator
                sb.Append(GridView1.Rows[i].Cells[k].Text + ',');
            }
            //append new line
            sb.Append("\r\n");
        }
        Response.Output.Write(sb.ToString());
        Response.Flush();
        Response.End();
    }
    

    for more information visit Here Hope it will help you

    0 讨论(0)
  • 2020-12-08 17:31

    First thanks to Devjosh for the good answer which I modified to work with gridviews that have AutoGenerateColumns=true and AllowSorting=true. Plus I stripped out any returned commas from the data to make sure csv file was not corrupted.

    private void ExportReport()
    {
        // set the resulting file attachment name to the name of the report...
        string fileName = "test";
    
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=" + fileName + ".csv");
        Response.Charset = "";
        Response.ContentType = "application/text";
    
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
    
        // Get the header row text form the sortable columns
        LinkButton headerLink = new LinkButton();
        string headerText = string.Empty;
    
        for (int k = 0; k < gvReport.HeaderRow.Cells.Count; k++)
        {
            //add separator
            headerLink = gvReport.HeaderRow.Cells[k].Controls[0] as LinkButton;
            headerText = headerLink.Text;
            sb.Append(headerText + ",");
        }
        //append new line
        sb.Append("\r\n");
        for (int i = 0; i < gvReport.Rows.Count; i++)
        {
            for (int k = 0; k < gvReport.HeaderRow.Cells.Count; k++)
            {
                //add separator and strip "," values from returned content...
    
                sb.Append(gvReport.Rows[i].Cells[k].Text.Replace(",", "") + ",");
            }
            //append new line
            sb.Append("\r\n");
        }
        Response.Output.Write(sb.ToString());
        Response.Flush();
        Response.End();
    }
    
    0 讨论(0)
  • 2020-12-08 17:47
    private void ExportGridToCSV()
            {            
                Response.Clear();
                Response.Buffer = true;
                Response.AddHeader("content-disposition", "attachment;filename=Employee.csv");
                Response.Charset = "";
                Response.ContentType = "application/text";
                GridEmployee.AllowPaging = false;
                GridEmployee.DataBind();
    
                StringBuilder columnbind = new StringBuilder();
                for (int k = 0; k < GridEmployee.Columns.Count; k++)
                {
    
                    columnbind.Append(GridEmployee.Columns[k].HeaderText + ',');
                }
    
                columnbind.Append("\r\n");
                for (int i = 0; i < GridEmployee.Rows.Count; i++)
                {
                    for (int k = 0; k < GridEmployee.Columns.Count; k++)
                    {
    
                        columnbind.Append(GridEmployee.Rows[i].Cells[k].Text + ',');
                    }
    
                    columnbind.Append("\r\n");
                }
                Response.Output.Write(columnbind.ToString());
                Response.Flush();
                Response.End();
    
            }
    

    Just call this method in a button Click event. For more Code, click the link Export gridview data into CSV file

    I hope this helps you. Thanks.

    0 讨论(0)
提交回复
热议问题