1 Reply Latest reply on May 27, 2009 6:18 AM by nikos101

    Advanced datagrid to  csv exporter:

    nikos101 Level 2

      I've used the code here

       

      http://www.abdulqabiz.com/blog/archives/2007/08/03/datagriddataexporter-export-datagrid-da ta-as-csv/

       

      to create a advanced datagrid to  csv exporter:

       

       

      However I now have grouped collections and would like to print all the children of a group, only to the first child level(for simplicities sake).

       

      Can anyone advise how I would do this?

       

       

      package utilityClasses
      {
          /**
             _________________________________________________________________________________________ ________________________

       


             DataGridDataExporter is a util-class to export DataGrid's data into different format.
             @class DataGridDataExporter (public)
             @author Abdul Qabiz (mail at abdulqabiz dot com)
             @version 0.01 (2/8/2007)
             @availability 9.0+
             @usage<code>DataGridDataExporter.<staticMethod> (dataGridReference)</code>
             @example
             <code>
             var csvData:String = DataGridDataExporter.exportCSV (dg);
             </code>

       


             _________________________________________________________________________________________ _________________________

       


           */
          import mx.collections.IViewCursor;
          import mx.controls.AdvancedDataGrid;
          import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;

       

          public class AdvancedDataGridDataExporter
          {

       

              public static function exportCSV(dg:AdvancedDataGrid, csvSeparator:String=",", lineSeparator:String="\n"):String
              {
                  var data:String="";
                  var columns:Array=dg.columns;
                  var columnCount:int=columns.length;
                  var column:AdvancedDataGridColumn;
                  var header:String="";
                  var headerGenerated:Boolean=false;
                  var dataProvider:Object=dg.dataProvider;
                  var rowCount:int=dataProvider.length;
                  var dp:Object=null;
                  var cursor:IViewCursor=dataProvider.createCursor();
                  var j:int=0;
      //loop through rows
                  while(!cursor.afterLast)
                  {
                      var obj:Object=null;
                      obj=cursor.current;
      //loop through all columns for the row
                      for(var k:int=0; k < columnCount; k++)
                      {
                          column=columns[k];
      //Exclude column data which is invisible (hidden)
                          if (!column.visible)
                          {
                              continue;
                          }
                          data+="\"" + column.itemToLabel(obj) + "\"";
                          if (k < (columnCount - 1))
                          {
                              data+=csvSeparator;
                          }
      //generate header of CSV, only if it's not genereted yet
                          if (!headerGenerated)
                          {
                              header+="\"" + column.headerText + "\"";
                              if (k < columnCount - 1)
                              {
                                  header+=csvSeparator;
                              }
                          }
                      }
                      headerGenerated=true;
                      if (j < (rowCount - 1))
                      {
                          data+=lineSeparator;
                      }
                      j++;
                      cursor.moveNext();
                  }
      //set references to null:
                  dataProvider=null;
                  columns=null;
                  column=null;
                  return (header + "\r\n" + data);
              }
          }
      }