【问题标题】:Export gridview data into CSV file将 gridview 数据导出为 CSV 文件
【发布时间】:2011-10-04 05:03:58
【问题描述】:

我在 ASP.Net 2.0 中有一个 gridview 控件,我需要将此 gridview 数据导出到 CSV 文件中。

我已将此 gridview 与数据集绑定。将数据集绑定到 gridview 后,我对 gridview 数据进行了一些更改,例如如果我在数据集中得到 0,那么我在 gridview 中将 0 显示为“已启动”,如果我得到了1 在数据集中,然后我在 gridview 中将 1 显示为“未开始”。

所以,我不能直接使用数据集进行导出。我需要的是..我想要将我的 gridview 数据(不是数据集的数据)导出到 CSV 文件的代码(在 c# 中)。

【问题讨论】:

    标签: c# asp.net gridview csv


    【解决方案1】:

    试试下面的代码,我已经用过很多次了。它将数据直接从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();
    }
    

    欲了解更多信息,请访问Here 希望对你有帮助

    【讨论】:

    • 谢谢,我试过了,但只获取标题列名称而不是这些列中的数据。
    • for (int i = 0; i
    • 我得到的列数大于 0,但同样的问题我没有得到大于 0 的行数。
    • 你能填满你的 girdview,因为如果你的 gridview 中有行,那么 for (int i = 0; i
    • 输出中没有数据的问题。删除以下行GridView1.AllowPaging = false; GridView1.DataBind(); 然后效果很好!感谢您的帖子。
    【解决方案2】:

    首先感谢 Devjosh 的好答案,我修改它以使用具有 AutoGenerateColumns=true 和 AllowSorting=true 的网格视图。另外,我从数据中删除了所有返回的逗号,以确保 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();
    }
    

    【讨论】:

    • 这不适用于 Devjosh 的多个页面
    • @fubo 要使用多个页面,只需添加 gvReport.AllowPaging = false; gvReport.DataBind();到方法的开头。
    【解决方案3】:
    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();
    
            }
    

    只需在按钮 Click 事件中调用此方法。 更多代码,请点击链接 Export gridview data into CSV file

    我希望这对你有帮助。谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多