Articles → ASP .NET GRIDVIEW → Export Grid View Data To CSV In Asp.Net
Export Grid View Data To CSV In Asp.Net
Software Requirement
- Visual studio 2005(or above) is installed on your machine.
- SQL Server 2005(or above) is installed on your machine
Prerequisite Knowledge
- Basics about SQL objects like tables.
- How to create tables in SQL server and how to insert data into the table?
- How to create a project using visual studio?
- Basic knowledge about basic controls like button textbox etc and their events
- Basic knowledge about data controls like grid view.
- How to bind data in grid view?
Steps Of Execution
- Create a new table
- Add data in the table
- Create a new project
- Add controls on the page
- Add code to bind grid
- Add code to export grid view data to CSV
Create A New Table
Click to Enlarge
Add Data In The Table
Click to Enlarge
Create A New Project
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" />
- Gridview (gvReport) will display data fetch from database.
- Button (btnGetData) – Get data from database and bind gridview(gvReport)
- 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
Click to Enlarge
Click to Enlarge
Click to Enlarge