DataTable dt = new DataTable(); for (int c = 0; c < headers.Length; c++) { dt.Columns.Add(headers[c], typeof(String)); } for (int r = 0; r < info.Count; r++) { String[] this_row = info[r]; dt.Rows.Add(this_row); } StringBuilder sb = new StringBuilder(); IEnumerable<string> columnNames = dt.Columns.Cast<DataColumn>().Select(column => column.ColumnName); sb.AppendLine(string.Join(",", columnNames)); foreach (DataRow row in dt.Rows) { IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString()); sb.AppendLine(string.Join(",", fields)); } String path = HttpContext.Current.Server.MapPath("~/" + "testcsv" + ".csv"); File.WriteAllText(path, sb.ToString()); Response.Write(path); Response.Flush(); Response.End();
<pre lang="cs">function convert() { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { var innerhtml = xmlhttp.responseText; window.location = innerhtml; } } xmlhttp.open("GET", "test_csv.aspx?is_ajax=1&function=convert", true); xmlhttp.send(); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)