Articles → ASP .NET GRIDVIEW → Export Grid View Data To CSV In Asp.Net

Export Grid View Data To CSV In Asp.Net






Software Requirement




  1. Visual studio 2005(or above) is installed on your machine.
  2. SQL Server 2005(or above) is installed on your machine

Prerequisite Knowledge




  1. Basics about SQL objects like tables.
  2. How to create tables in SQL server and how to insert data into the table?
  3. How to create a project using visual studio?
  4. Basic knowledge about basic controls like button textbox etc and their events
  5. Basic knowledge about data controls like grid view.
  6. How to bind data in grid view?

Steps Of Execution




  1. Create a new table
  2. Add data in the table
  3. Create a new project
  4. Add controls on the page
  5. Add code to bind grid
  6. Add code to export grid view data to CSV



Create A New Table




Picture showing the database table in the design mode
Click to Enlarge


Add Data In The Table




Picture showing adding the data in the table
Click to Enlarge


Create A New Project




Picture showing the project explorer in the solution explorer of the visual studio
Click to Enlarge


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" />
     
    
<asp:Button ID="btnExportGridToCSV" runat="server" Text="Export" OnClick="btnExportGridToCSV_Click" />


  1. Gridview (gvReport) will display data fetch from database.
  2. Button (btnGetData) – Get data from database and bind gridview(gvReport)
  3. Button (btnExportGridToCSV) – export gridview to CSV.

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




Picture showing the page when loaded for the first time
Click to Enlarge



Picture showing the data bound to the gridview on click of the Get Data button
Click to Enlarge



Picture showing the data exported to excel
Click to Enlarge


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

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250