Articles → ASP .NET GRIDVIEW → Export grid view data to CSV in asp.net


























Software requirement









Prerequisite knowledge

















Steps of execution



















Create a new table







Add data in the table







Create a new project







Add controls on the page




<asp:GridView ID="gvReport" runat="server"  AutoGenerateColumns="false">
    <HeaderStyle Font-Names="Verdana" Font-Size="Small" BackColor="#90546f" ForeColor="White" BorderStyle="Solid" BorderColor="Black" />
    <RowStyle Font-Names="Verdana" Font-Size="Small"/>
        <Columns>
            <asp:TemplateField HeaderText="Course Name">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%#Eval("course_name") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Course Code">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%#Eval("course_code") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Due Date">
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%#Eval("due_date") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:Button ID="btnGetData" runat="server" Text="Get Data" OnClick="btnGetData_Click" />
    &nbsp;
    <asp:Button ID="btnExportGridToCSV" runat="server" Text="Export" OnClick="btnExportGridToCSV_Click" />









Add code to bind grid




protected void btnGetData_Click(object sender, EventArgs e)
{
  DataSet oDataSet = new DataSet();
  using (SqlConnection oConnection = new SqlConnection("connection_string"))
  {
      // Open a connection
      oConnection.Open();
      using (SqlDataAdapter adapter = new SqlDataAdapter("select * from Course", oConnection))
      {

                adapter.Fill(oDataSet);
      }
  }

  gvReport.DataSource = oDataSet.Tables[0];
  gvReport.DataBind();        
}





Add code to export grid view data to CSV




protected void btnExportGridToCSV_Click(object sender, EventArgs e)
{
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition",
         "attachment;filename=GridViewExport.csv");
        Response.Charset = "";
        Response.ContentType = ".csv";

        
        StringBuilder sb = new StringBuilder();
        for (int k = 0; k < gvReport.Columns.Count; k++)
        {
            //add separator
            sb.Append(gvReport.Columns[k].HeaderText + ',');
        }
        //append new line
        sb.Append("\r\n");
        for (int i = 0; i < gvReport.Rows.Count; i++)
        {
            sb.Append(((Label)gvReport.Rows[i].FindControl("Label1")).Text + ',');
            sb.Append(((Label)gvReport.Rows[i].FindControl("Label2")).Text + ',');
            sb.Append(((Label)gvReport.Rows[i].FindControl("Label3")).Text);
            
            //append new line
            sb.Append("\r\n");
        }
        Response.Output.Write(sb.ToString());
        Response.Flush();
        Response.Close();
}





Output















Posted By  -  Karan Gupta
 
Posted On  -  Tuesday, April 16, 2013

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250