Export Dojo Datagrid Results to .csv File?

8471
23
01-09-2012 05:50 AM
EmilyLaMunyon
Occasional Contributor
Hello,

Does anyone have advice on how to export the contents of a dojo.datagrid to a .csv file or excel spreadsheet? I am needing this function in my Javascript API web mapping application.

Thanks!
0 Kudos
23 Replies
KellyHutchins
Esri Frequent Contributor
I haven't tested this but it looks like the following sample from ArcScripts may do what you need:

http://arcscripts.esri.com/details.asp?dbid=16528
0 Kudos
DeanSeales
New Contributor
I there an updated version of that sample?
0 Kudos
GeorgeSimpson
Occasional Contributor
You could also look at using the dojox.grid.EnhancedGrid which has "plugins" that can export to various formats
0 Kudos
EmilyLaMunyon
Occasional Contributor
Hi,

Has anyone had luck exporting a dojo datagrid to a csv file? The example above is a bit outdated and I am having trouble getting it to work.:confused:

Thanks!
0 Kudos
HemingZhu
Occasional Contributor III
Hi,

Has anyone had luck exporting a dojo datagrid to a csv file? The example above is a bit outdated and I am having trouble getting it to work.:confused:

Thanks!


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.
0 Kudos
EmilyLaMunyon
Occasional Contributor
Thanks again for your help!!

Do you know if this script works for a Dojo datagrid?
0 Kudos
HemingZhu
Occasional Contributor III
Thanks again for your help!!

Do you know if this script works for a Dojo datagrid?


Yes, basically the data store for datagrid is either the query, find or identify featureset. I used the script in a Windows Phone 7 app where i convert a query result FeatureSet to csv or .xls based on user choice.
0 Kudos
EmilyLaMunyon
Occasional Contributor
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> 


0 Kudos
MattMoyles
New Contributor III
A CSV output is possibly the simplest you could have. Take the datastore backing your grid. Query it. Loop the results, and concatenate a new line to some output variable. Post the data to a server-side script. (PHP or ASP.NET) that will write your data to a file and then redirect the user's browser to download the .csv file.

Here's some javascripty psedo-code.

itemStore //data store backing your grid
var queryResults = itemStore.query();

var output = '';
dojo.forEach(queryResults, function(result) {
    output = output + '\n' + result['col1'] + ',' + result['col2'] + ',' + result['col3'] + and so on...
});

xhr.post(/*post to some serverside script*/);
xhr.onSuccess(/*redirect them to download the file*/);




Piece of cake.

EDIT: Here found this for ya too. http://stackoverflow.com/questions/4440803/exporting-a-dojo-datagrid-to-a-csv-file
0 Kudos