I am getting close. The problem is that only one result, the last one, from my datagrid is showing up in my text area for export to excel. I think the csv is not getting the full results from the feature selected, for some reason it only gets one. How do I get the full results of the selection ready for csv export?function executeIdentifyTask(geom) { //clear the graphics layer map.graphics.clear(); identifyParams.geometry = geom; identifyParams.mapExtent = map.extent; identifyTask.execute(identifyParams,function(response){ var polygonSymbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([98,194,204]), 2), new dojo.Color([98,94,204,0.8])); var markerSymbol = new esri.symbol.SimpleMarkerSymbol().setColor(new dojo.Color([25,50,225,0.3])); var controlItems = []; var surveyItems = []; dojo.forEach(response,function(result){ var feature = result.feature; if (result.layerName =="surveys"){ showSurveysNameGrid(); feature.setSymbol(polygonSymbol); var attributes = feature.attributes; surveyItems.push(attributes); var csvTextSurv = "DOCUMENT_N;TOWNSHIP_RANGE;SECTION\n"; csvTextSurv += attributes["DOCUMENT_N"] + ";" + attributes["TOWNSHIP_RANGE"] + ";" + attributes["SECTION"] + "\n"; document.getElementById("csvSurv").value = csvTextSurv; map.graphics.add(feature); }else{ showPointNameGrid(); feature.setSymbol(markerSymbol); var attributes = feature.attributes; controlItems.push(feature.attributes); var csvTextMons = "POINT_NAME;SECTION\n"; csvTextMons += attributes["POINT_NAME"] + ";" + attributes["TOWNSHIP_RANGE"] + ";" + attributes["SECTION"] + "\n"; } document.getElementById("csvMons").value = csvTextMons; map.graphics.add(feature); }); if(surveyItems.length >0){ showSurveysNameGrid(); var surveysStore = new dojo.data.ItemFileReadStore({data:{identifier:'DOCUMENT_N',items:surveyItems}}); var grid = dijit.byId('grid5'); grid.setStore(surveysStore); } if(controlItems.length >0){ showPointNameGrid(); var controlStore = new dojo.data.ItemFileReadStore({data:{identifier:'POINT_NAME',items:controlItems}}); var grid = dijit.byId('grid4'); grid.setStore(controlStore); } }); } <div id="footerPoints" class="roundedCorners" dojotype="dijit.layout.ContentPane" region="bottom" style=" padding-bottom:5%; height:18%; display:none"> <form action="http://surveyor.slco.org/JSAPIExportToExcel/DojoGridToExcel.asmx/CSV2Excel " method="POST"> <textarea name="csv" id="csvMons" style="width: 500px; height:30px"></textarea> <input type="submit" value="Submit" /> </form> <table dojotype="dojox.grid.DataGrid" data-dojo-id="grid4" id="grid4" data-dojo-props="rowsPerPage:'5', rowSelector:'20px'"> <thead> <tr> <th field="POINT_NAME" width="200px" > Point Name </th> <th field="GRID_ADDRESS" width="200px" > Address </th> <th field='TOWNSHIP_RANGE' width='200px'>Township/Range</th> <th field="SECTION" width="200px" > Section </th> <th fields="POINT_NAME,LONGITUDE_DD,LATITUDE_DD" formatter="makeLink1" width="200px" > Monument Reference Sheet </th> <th field="MON_NOTES" width="200px" > Monument Notes </th> </tr> </thead> </table> </div> <div id="footersurvName" class="roundedCorners" dojotype="dijit.layout.ContentPane" region="bottom" style=" padding-bottom:5%; height:18%; display:none"> <form action="http://surveyor.slco.org/JSAPIExportToExcel/DojoGridToExcel.asmx/CSV2Excel " method="POST"> <textarea name="csv" id="csvSurv" style="width: 500px; height:30px"></textarea> <input type="submit" value="Submit" /> </form> <table dojotype="dojox.grid.DataGrid" data-dojo-id="grid5" id="grid5" data-dojo-props="rowsPerPage:'5', rowSelector:'20px'"> <thead> <tr> <th field="DOCUMENT_NUM" width="200px" > Survey Number </th> <th field="SURVEYOR" width="200px" > Surveyor </th> <th field="ADDRESS_OF_SURVEY" width="200px" > Address </th> <th field='TOWNSHIP_RANGE' width='200px'> Township/Range </th> <th field="SECTION" width="200px" > Section </th> <th fields="subdir,page_id" formatter="makeLink" width="200px" > PDF </th> </tr> </thead> </table> </div>
function executeIdentifyTask(geom) { //clear the graphics layer map.graphics.clear(); identifyParams.geometry = geom; identifyParams.mapExtent = map.extent; identifyTask.execute(identifyParams,function(response){ var polygonSymbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([98,194,204]), 2), new dojo.Color([98,94,204,0.8])); var markerSymbol = new esri.symbol.SimpleMarkerSymbol().setColor(new dojo.Color([25,50,225,0.3])); var controlItems = []; var surveyItems = []; dojo.forEach(response,function(result){ var feature = result.feature; alert(feature.attributes); if (result.layerName =="surveys"){ showSurveysNameGrid(); feature.setSymbol(polygonSymbol); //searchType="selSurvey"; //for (var i = 0; i < feature.length; i++) { //alert(result.feature); var attributes = feature.attributes; surveyItems.push(attributes); var csvTextSurv = "DOCUMENT_N;TOWNSHIP_RANGE;SECTION\n"; csvTextSurv += attributes["DOCUMENT_N"] + ";" + attributes["TOWNSHIP_RANGE"] + ";" + attributes["SECTION"] + "\n"; document.getElementById("csvSurv").value = csvTextSurv; map.graphics.add(feature); }
Heming,
Before I use the example you provided, I am trying to use the following sample. I have it working, the problem is that only one result is being exported from the data store. I think it has to do with the feature attributes returning only one result. Does an Identify Task return a Feature Set?
http://arcscripts.esri.com/details.asp?dbid=16528function executeIdentifyTask(geom) { //clear the graphics layer map.graphics.clear(); identifyParams.geometry = geom; identifyParams.mapExtent = map.extent; identifyTask.execute(identifyParams,function(response){ var polygonSymbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([98,194,204]), 2), new dojo.Color([98,94,204,0.8])); var markerSymbol = new esri.symbol.SimpleMarkerSymbol().setColor(new dojo.Color([25,50,225,0.3])); var controlItems = []; var surveyItems = []; dojo.forEach(response,function(result){ var feature = result.feature; alert(feature.attributes); if (result.layerName =="surveys"){ showSurveysNameGrid(); feature.setSymbol(polygonSymbol); //searchType="selSurvey"; //for (var i = 0; i < feature.length; i++) { //alert(result.feature); var attributes = feature.attributes; surveyItems.push(attributes); var csvTextSurv = "DOCUMENT_N;TOWNSHIP_RANGE;SECTION\n"; csvTextSurv += attributes["DOCUMENT_N"] + ";" + attributes["TOWNSHIP_RANGE"] + ";" + attributes["SECTION"] + "\n"; document.getElementById("csvSurv").value = csvTextSurv; map.graphics.add(feature); }
There is a python script in http://resources.arcgis.com/gallery/file/geoprocessing/details?entryID=95009B25-1422-2418-7FB5-B8638.... You can easily modify it and use it as a GP Service. I personally used it to convert a query results (FeatureSet) to CSV or .XLS file.
function exportCSVData() { var gridData = dijit.byId("grid"); var csvdatafield = dojo.byId("csvdata"); 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; ifldCnt++) { fieldValue = ""; //columnVal = this.getValuefromAttribute(featureAttribute[fields[ifldCnt].field], false, ""); 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"; } if (csvText != "") csvText = escape(csvText); csvdatafield.value = csvText; // After value is set for the hidden field submit the form var formCSV = dojo.byId("csvForm"); formCSV.submit(); } }
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; System.Collections.Specialized.NameValueCollection parameters = context.Request.Form; string moduleName = parameters["module"]; ExportHelper objExportCSVHelper = new ExportHelper(); objExportCSVHelper.ExportToCSV(parameters["csvdata"].ToString(), HttpContext.Current, parameters["title"].ToString()); }
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(); } //Since we used escape to send text from javascript to Server side we need this function 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; }
Are you using the python script tabletoexcel.py that i pointed to? if so, here is how i modified it to fit my use. I added Make Feature Layer (or Make Table View) with the Query Expression (the same as you use query task's where clause). then use the result feature layer or table view (in your case would be data store) as the first input for the tabletoexcel.py and published it as a GP service (i also include a zip module to zip the result).
I have taken this approach for exporting the data to csv from datagrid.
Added a form in page with hidden attribute and set its action to handler
[HTML]<form id="csvForm" action="Handlers/MyWebHandler.ashx?module=ExportToExcel" method="post">
<input type="hidden" value="" id="csvdata" name="csvdata" />
<input type="hidden" value="" id="title" name="title" />
</form>[/HTML]
Then in js function is written to get the grid data.function exportCSVData() { var gridData = dijit.byId("grid"); var csvdatafield = dojo.byId("csvdata"); 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; ifldCnt++) { fieldValue = ""; //columnVal = this.getValuefromAttribute(featureAttribute[fields[ifldCnt].field], false, ""); 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"; } if (csvText != "") csvText = escape(csvText); csvdatafield.value = csvText; // After value is set for the hidden field submit the form var formCSV = dojo.byId("csvForm"); formCSV.submit(); } }
Once the form is submitted, the control is passed to handler, that will fetch the data from form submitted and create csv
In handler get the attributes "MyWebHandler.ashx"public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; System.Collections.Specialized.NameValueCollection parameters = context.Request.Form; string moduleName = parameters["module"]; ExportHelper objExportCSVHelper = new ExportHelper(); objExportCSVHelper.ExportToCSV(parameters["csvdata"].ToString(), HttpContext.Current, parameters["title"].ToString()); }
And in ExportHelper class write the ExportToCSV Functionpublic 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(); } //Since we used escape to send text from javascript to Server side we need this function 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; }
Could you please provide all the code for your MyWebHandler.ashx file? I am trying to implement your strategy but keep getting errors in that file. I assume I missed some code or something so if you could provide the entire code for that file it would be greatly appreciated.
Thanks,
Mark
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); }
function exportGridCSV() { dataArray.length = 0; var fieldValue; var fieldNames = []; //qTaskNameList created from query task that populated the grids dojo.forEach(qTaskNameList, function(gridName) { var gridData = dijit.byId(gridName+"_grid"); var gridLength = gridData.rowCount; var fields = gridData.layout.cells; var fieldNames.length = 0; //populates an array with the field names used to find the values in the data store dojo.forEach(fields, function (field) { fieldNames.push(field.field); dataArray.push(field.field); }); dataArray.push("\r");//first line is the field names if (gridLength > 0) {//don't try to export if there were not values in that grid var gridStore = gridData.store._arrayOfAllItems;//the store of one grid //for each record, populates the array with the values for the fields stored in the field array for (var i=0; i< gridStore.length; i++) { var gridRec = gridStore; var j = 0; dojo.forEach (fieldNames, function(fieldName) { fieldValue = gridRec[fieldName]; if (isNaN(fieldValue)) {//for data containing slashes if (fieldValue.indexOf('\'') != -1 || fieldValue.indexOf('\"') != -1 || fieldValue.indexOf(',') != -1) { if (fieldValue.indexOf('\"') != -1) { fieldValue = fieldValue.replace("\"", "\"\""); } fieldValue = "\"" + fieldValue + "\""; } } dataArray.push(fieldValue); j++; }); dataArray.push("\r"); }//end of record dataArray.push("\r"); //puts an empty row before the next set of records } }); submitCSVprint(); } function submitCSVprint() { //opens the data in a hidden form used for printing and allows the user to either open or save the file. var url = "webservices/csv.ashx"; var data = dataArray.join(); var f = dojo.byId("downloadForm"); f.action = url; dojo.byId("reportinput").value = data; var distType = dijit.byId("distTypeSelect").value; var distNum = dojo.byId("txtDistNumber").value; dojo.byId("filename").value = distType+"_Dist_"+distNum+"_"; f.submit(); }