public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; System.Collections.Specialized.NameValueCollection parameters = context.Request.Form; string moduleName = parameters["module"]; string requestFeedback = ""; switch (moduleName) { case "ExportToExcel": EgisWeb.ExportHelper objExportCSVHelper = new EgisWeb.ExportHelper(); objExportCSVHelper.CSV2Excel(parameters["csvdata"].ToString(), HttpContext.Current, parameters["title"].ToString()); break; } if (moduleName != "ExportToExcel") context.Response.Write(requestFeedback); } public void CSV2Excel(string strCSV, HttpContext current, string strTitle) { current.Response.Clear(); strCSV = removeUniCodeSequenceCharacters(strCSV); strCSV = System.Text.RegularExpressions.Regex.Unescape(strCSV); current.Response.AddHeader("content-disposition", "attachment;filename=" + strTitle +"_"+DateTime.Now.ToString("ddmmyyyyHHMMSS") + ".csv"); current.Response.Charset = ""; current.Response.ContentType = "application/octet-stream"; current.Response.Write(strCSV); current.Response.End(); } private string removeUniCodeSequenceCharacters(string strWithEscapeSequence) { string strUse = ""; strUse = strWithEscapeSequence.Replace("%20", " "); strUse = strUse.Replace("%2C", ","); strUse = strUse.Replace("%27", "'"); strUse = strUse.Replace("%22", "\""); strUse = strUse.Replace("%3F", "?"); strUse = strUse.Replace("%0A", "\n"); strUse = strUse.Replace("%28", "("); strUse = strUse.Replace("%29", ")"); strUse = strUse.Replace("%26", "&"); strUse = strUse.Replace("%23", "#"); strUse = strUse.Replace("%3A", ":"); strUse = strUse.Replace("%21", "!"); strUse = strUse.Replace("%2D", "-"); strUse = strUse.Replace("%3B", ";"); strUse = strUse.Replace("%2A", "*"); strUse = strUse.Replace("%2B", "+"); strUse = strUse.Replace("%2E", "."); strUse = strUse.Replace("%3C", "<"); strUse = strUse.Replace("%3D", "="); strUse = strUse.Replace("%3E", ">"); strUse = strUse.Replace("%40", "@"); return strUse; }
Solved! Go to Solution.
<%@ WebHandler Language="C#" Class="csv" %> using System; using System.Web; public class csv : IHttpHandler { public void ProcessRequest (HttpContext context) { String content = "no data"; String filename = "MyFile"; if (context.Request["report"] != null) { try { content = context.Request["report"].ToString(); } catch { } } if (context.Request["filename"] != null) { try { filename = context.Request["filename"].ToString() + "_" + DateTime.Now.ToString("MMMd_HH.mm"); } catch { } } context.Response.ContentType = "text/csv"; context.Response.AddHeader("Content-disposition", "attachment;filename=" + filename + ".csv"); context.Response.Write(content); } public bool IsReusable { get { return false; } } }
function exportCSVData() { var gridData = dijit.byId("gridCorners"); var csvdatafield = dojo.byId("csvdata"); dijit.byId('btnExport').setAttribute('checked', false); var csvText = ""; var fields = gridData.structure[0].cells[0]; if (fields != null && fields.length > 0) { for (var ifldCnt = 0; ifldCnt < fields.length; ifldCnt++) { if (ifldCnt == 0) { csvText = fields[ifldCnt].name; } else { csvText += "," + fields[ifldCnt].name; } } csvText += "\n"; var fieldValue = ""; for (var i = 0; i < gridData.rowCount; i++) { var featureAttribute = gridData.store._arrayOfAllItems; var columnVal; for (var ifldCnt = 0; ifldCnt < fields.length - 1; ifldCnt++) { fieldValue = ""; columnVal = featureAttribute[fields[ifldCnt].field][0]; if (columnVal != null && columnVal != "") { fieldValue = columnVal; } if (isNaN(fieldValue)) { if (fieldValue.indexOf('\'') != -1 || fieldValue.indexOf('\"') != -1 || fieldValue.indexOf(',') != -1) { if (fieldValue.indexOf('\"') != -1) { fieldValue = fieldValue.replace("\"", "\"\""); } fieldValue = "\"" + fieldValue + "\""; } } if (ifldCnt == 0) { csvText += fieldValue; } else { csvText += "," + fieldValue; } } csvText += "\n"; } getCSV(csvText) } }
function getCSV(csvText) { //Get the stored CSV var url = "webservices/csv.ashx"; var data = csvText; var f = dojo.byId("downloadform"); f.action = url; dojo.byId("reportinput").value = data; userChoice = prompt("Choose a File Name (the date and time will be added to end of filename):"); if (userChoice != null && userChoice != 'undefined' && userChoice != '') { dojo.byId("filename").value = userChoice; f.submit(); } else if (userChoice == 'undefined') { alert("You must type in a filename!"); } else if (userChoice == '') { alert("You must type in a filename!"); } else if (userChoice == null) { alert("The CSV Export was Canceled"); } }
<%@ WebHandler Language="C#" Class="csv" %> using System; using System.Web; public class csv : IHttpHandler { public void ProcessRequest (HttpContext context) { String content = "no data"; String filename = "MyFile"; if (context.Request["report"] != null) { try { content = context.Request["report"].ToString(); } catch { } } if (context.Request["filename"] != null) { try { filename = context.Request["filename"].ToString() + "_" + DateTime.Now.ToString("MMMd_HH.mm"); } catch { } } context.Response.ContentType = "text/csv"; context.Response.AddHeader("Content-disposition", "attachment;filename=" + filename + ".csv"); context.Response.Write(content); } public bool IsReusable { get { return false; } } }
function exportCSVData() { var gridData = dijit.byId("gridCorners"); var csvdatafield = dojo.byId("csvdata"); dijit.byId('btnExport').setAttribute('checked', false); var csvText = ""; var fields = gridData.structure[0].cells[0]; if (fields != null && fields.length > 0) { for (var ifldCnt = 0; ifldCnt < fields.length; ifldCnt++) { if (ifldCnt == 0) { csvText = fields[ifldCnt].name; } else { csvText += "," + fields[ifldCnt].name; } } csvText += "\n"; var fieldValue = ""; for (var i = 0; i < gridData.rowCount; i++) { var featureAttribute = gridData.store._arrayOfAllItems; var columnVal; for (var ifldCnt = 0; ifldCnt < fields.length - 1; ifldCnt++) { fieldValue = ""; columnVal = featureAttribute[fields[ifldCnt].field][0]; if (columnVal != null && columnVal != "") { fieldValue = columnVal; } if (isNaN(fieldValue)) { if (fieldValue.indexOf('\'') != -1 || fieldValue.indexOf('\"') != -1 || fieldValue.indexOf(',') != -1) { if (fieldValue.indexOf('\"') != -1) { fieldValue = fieldValue.replace("\"", "\"\""); } fieldValue = "\"" + fieldValue + "\""; } } if (ifldCnt == 0) { csvText += fieldValue; } else { csvText += "," + fieldValue; } } csvText += "\n"; } getCSV(csvText) } }
function getCSV(csvText) { //Get the stored CSV var url = "webservices/csv.ashx"; var data = csvText; var f = dojo.byId("downloadform"); f.action = url; dojo.byId("reportinput").value = data; userChoice = prompt("Choose a File Name (the date and time will be added to end of filename):"); if (userChoice != null && userChoice != 'undefined' && userChoice != '') { dojo.byId("filename").value = userChoice; f.submit(); } else if (userChoice == 'undefined') { alert("You must type in a filename!"); } else if (userChoice == '') { alert("You must type in a filename!"); } else if (userChoice == null) { alert("The CSV Export was Canceled"); } }
Do you able to complete your task? Thanks.
Hi May,
I used another route to finish this work. However, I am not sure if my modified application fits your requirement. Basically, I have a table to display all the data and the filter window could provide users filter data by conditions users set. The post here is for users to drag a box and download the data in CSV/Excel format. However, there is a problem with the web server. I can not pass the website link.
Thanks Yang. I also got the same problem with the web.config file. I try to use EnhancedGrid and posted a thread here: Can't get export datagrid to csv
Which method do you use? Can you show me how do you do it? Thanks.
Hi May,
I am wondering if you would like a similar web application like the website (Gridx Export ). If yes, I could show you the workflow. However, if you prefer the user could drag the box and download the data in csv/excel format on your web map, I am still working on it.
Please let me know your opinions.
Hank
Actually I just need to export the search results to csv just like JSFiddle I modified:
I don't need to drag a box on a map to get the data to export to csv format. If you could, would be greatly appreciate.
Thanks.