0

I have three advanced datagrid whose data I need to export to excel. I have merged the grid's data into a single arraycollection.

Now I need to export that arraycollection to csv or excel format(.xlsx). The examples I found is all exporting datagrid to excel/csv. I need to know how to export arraycollection to csv.

Kindly Help.

1 Answers1

0

Since the CSV format is simple, you can just iterate over the array elements and create the file as String.

This is parta of an example code

    public function exportToCSVStr(data:Array,headersList:Array=null,headerExclude:Array=null):String{
            var result:String = "";
            for each(var line:String in this.exportToCSV(data,headersList,headerExclude)){
                result+=line;
            }   
            return result;  
        }


public  function  exportToCSV(data:Array,headersList:Array,headerExclude:Array):Array{
            if(headerExclude==null)headerExclude=[];

            var hedersToExclude:ArrayCollection = new  ArrayCollection(headerExclude);
            var result:ArrayList = new ArrayList();
            var headers:String = "";
            var hList:ArrayList = new ArrayList();
            var i:int=0;

            if(headersList==null){
                for(var h1:String in data[0]){
                    if(hedersToExclude.contains(h1))
                        continue;
                    if(i!=0){
                        headers+=",";
                    }
                    headers+=h1;
                    hList.addItem(h1);
                    i++;
                }
            }else{
                for each(var h2:* in headersList){
                    if(i!=0){
                        headers+=",";
                    }
                    headers+=h2["label"];
                    hList.addItem(h2["attr"]);
                    i++;
                }
            }
            headers+=newLineDelim;
            result.addItem(headers);


            for each(var item:* in data){
                var itemStr:String="";
                i=0;
                for each(var attr:String in hList.toArray()){
                    if(i!=0){
                        itemStr+=",";
                    }
                    itemStr+=toCSVString(item[attr]);
                    i++;
                }
                itemStr+=newLineDelim;
                result.addItem(itemStr);
            }

            return result.toArray();
        }   

protected  function toCSVString(data:*):String{
            if(data==null) 
                return "";
            if(data is Date){
                return  strDelim + dateFormatter.format(data) + strDelim;
            } else if(data is String){
                return  strDelim + data + strDelim;
            } else if(data is Number) {
                return   numberFormatter.format(data);
            }else {
                return data;
            }
        }

And then you can create the file with

var fr:FileReference = new FileReference();
    fr.save(csvUtil.exportToCSVStr(alist.toArray(),getCsvIncludeHeaders()),getFileNameTitle());

Davide

Panciz
  • 2,183
  • 2
  • 30
  • 54