简体   繁体   English

将 gridview 数据导出到 CSV 文件中

[英]Export gridview data into CSV file

I have a gridview control in ASP.Net 2.0 and i need to export this gridview data into CSV file.我在 ASP.Net 2.0 中有一个 gridview 控件,我需要将此 gridview 数据导出到 CSV 文件中。

I have bind this gridview with the dataset.After binding the dataset to the gridview i have done some changes in the gridview data like if I got 0 in dataset then i show 0 as "Started" in the gridview and if i got 1 in the dataset then I show 1 as "Not Started" in the gridview. I have bind this gridview with the dataset.After binding the dataset to the gridview i have done some changes in the gridview data like if I got 0 in dataset then i show 0 as "Started" in the gridview and if i got 1 in the然后我在 gridview 中将 1 显示为“未开始”。

So, i can't use dataset directly for exporting.所以,我不能直接使用数据集进行导出。 What i need is.. i want the code (in c#) that export my gridview data(not dataset's data) into CSV file.我需要的是..我想要将我的 gridview 数据(不是数据集的数据)导出到 CSV 文件的代码(在 c# 中)。

Try following code, i used it already many times.试试下面的代码,我已经用过很多次了。 It will export the data directly from gridview to csv file specified in the code.它将直接从 gridview 导出数据到代码中指定的 csv 文件。

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欲了解更多信息,请访问这里希望它会帮助你

First thanks to Devjosh for the good answer which I modified to work with gridviews that have AutoGenerateColumns=true and AllowSorting=true.首先感谢 Devjosh 的好答案,我修改它以使用具有 AutoGenerateColumns=true 和 AllowSorting=true 的网格视图。 Plus I stripped out any returned commas from the data to make sure csv file was not corrupted.另外,我从数据中删除了所有返回的逗号,以确保 csv 文件没有损坏。

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();
}
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.只需在按钮 Click 事件中调用此方法。 For more Code, click the link Export gridview data into CSV file更多代码,点击链接Export gridview data into CSV file

I hope this helps you.我希望这可以帮助你。 Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM