protected void btnExport_Click(object sender, EventArgs e) { ExportCSV(); } protected void ExportCSV() { GridViewSW.DataSource = ViewState["source"]; Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=Orders.csv"); Response.Charset = ""; Response.ContentType = "application/text"; GridViewSW.AllowPaging = false; GridViewSW.DataBind(); StringBuilder columnbind = new StringBuilder(); for (int k = 0; k < GridViewSW.Columns.Count; k++) { columnbind.Append(GridViewSW.Columns[k].HeaderText + ","); } columnbind.Append("\r\n"); for (int i = 0; i < GridViewSW.Rows.Count; i++) { for (int j = 0; j < GridViewSW.Columns.Count; j++) { columnbind.Append(GridViewSW.Rows[i].Cells[j].Text + ","); } columnbind.Append("\r\n"); } Response.Output.Write(columnbind.ToString()); Response.Flush(); Response.End(); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)