Published in: C#
protected void outputCSV(DataTable dtResults) { bool boolFirstItem = true; // Whether this is the first item in the data // Set the output to the an Excel CSV (Comma Seperated Value) file Response.AppendHeader("content-disposition", "attachment; filename=RegistrationUsersExport.csv"); Response.ContentType = "application/vnd.ms-excel"; // Output the Column Names for (int i = 0; i < dtResults.Columns.Count; i++) { // Handle placing commas between the items if (boolFirstItem) boolFirstItem = false; else Response.Write(","); // Output the column name Response.Write(dtResults.Columns[i].ColumnName); } Response.Write("\n"); // Read the data and export the values foreach (DataRow drResults in dtResults.Rows) { // Output the Column Values boolFirstItem = true; for (int i = 0; i < dtResults.Columns.Count; i++) { // Handle placing commas between the items if (boolFirstItem) boolFirstItem = false; else Response.Write(","); // Output the column value Response.Write("\"" + drResults[i].ToString() + "\""); } Response.Write("\n"); } Response.End(); }
You need to login to post a comment.
