Select to view content in your preferred language

Data Grid export to excel or csv

3371
2
06-18-2014 06:56 AM
deleted-user-yA_w_FC9FKe5
Deactivated User
Not sure how to go about this.  We need to be able to export the data in the data grid to excel or csv.  Our application will be used on PC and tablets.  I thought the EnhancedGrid might be the answer with the dojox.grid.enhanced.plugins.exporter.CSVWriter.  However, I'm not having much luck. 
  
I thought I would just need to do this since my datagrid was already loaded with the correct data:

1.) Add  plugins { exporter: true } to my datagrid but not sure how to do this.

2.)  Add this functions to the script

function exportAll(){
    dijit.byId("gridCE").exportGrid("csv", function(str){
        dojo.byId("output").value = str;
    });
};
function exportSelected(){
    var str = dijit.byId("gridCE").exportSelected("csv");
    dojo.byId("output").value = str;
};

3.) Create buttons
<button onclick="exportAll()">Export all to CSV</button>
<button onclick="exportSelected()">Export Selected Rows to CSV</button>

4.) Add references
"dojox/grid/EnhancedGrid",
"dojox/data/CsvStore",
"dojox.grid.enhanced.plugins.exporter.CSVWriter"


Current Data Grid.

<button onclick="exportAll()">Export all to CSV</button>
<button onclick="exportSelected()">Export Selected Rows to CSV</button>


<table  data-dojo-type="dojox.grid.EnhancedGrid" jsid="gridCE" id="gridCE" selectionMode="single">
                 <thead>
                            <tr>

       
              <th field="FID" formatter="makeZoomButton" width="30px">
                <img alt="+" src="assets/images/GenericSearch32.png"/>
              </th>      
         <th field="FID" width="100px">FID</th>     
                                   <th field="DIVISIONNA" width="100px">DIVISIONNA</th>
                                   <th field="GSF" width="60px">GSF</th>
         <th field="OPENED" width="100px">OPENED</th>
         <th field="TYPE" width="70px">TYPE</th>
         <th field="MALLNAME" width="200px">MALLNAME</th>
         <th field="MALL_GRADE" width="70px">MALL GRADE</th>                                  
         <th field="DISTRICT" width="150px">DISTRICT</th>
                                   <th field="MACYS_MALL" width="100px">MACYS_MALL</th> 
                                   <th field="WebLon_X" width="100px">WebLon_X</th>                
                                   <th field="WebLat_Y" width="100px">WebLat_Y</th>                              
                            </tr>
                  </thead>
                            </table>






Shortened code for putting into data grid:

var emptyCells = { items: "" };
var emptyStore = new dojo.data.ItemFileWriteStore({data: emptyCells});
   grid = dijit.byId('gridCE');
      grid.setStore(emptyStore);


    //Put values in array for datagrid
     var attValues = resultFeatures.attributes;      
               dataForGrid.push(attValues);

     }; 
    var data = {
  identifier : "FID",
  label : "FID",
        items : dataForGrid
        };

    var store = new dojo.data.ItemFileReadStore({data: data});
 grid.setSortIndex(2,"true"); //sort on the state name  
 
 grid.setStore(store);
0 Kudos
2 Replies
KenBuja
MVP Esteemed Contributor
This site (unfortunately, as of today, the services seem to be down) has the option to export CVS files from dGrids. The code for that is in the function "saveGridCSV" if you examine the source code.
0 Kudos
deleted-user-yA_w_FC9FKe5
Deactivated User
Thanks Ken.  I took a look at that but that code looks pretty old and with the example not working I've decided to go in another direction.  You would think the enhancedgrid export piece would be simple with already having the data loaded in the grid. 

hmmm.  Any suggestions anyone???
0 Kudos