Not yet rated

Problem

I need to load external xml values to a datagrid, but i can't export the values into Excel format.

Solution

The easiest and quickest solution is to export the data out to CSV, which is readable by Excel, Notepad, and many other programs. I recommend using csvlib to make your live a little easier for handling the CSV.

Detailed explanation

This whole example is based on using csvlib (http://code.google.com/p/csvlib/) which is a great library for everything csv.  Check it out.

Let's start with some sample data

[Bindable]
            private var sampleData:ArrayCollection = new ArrayCollection([
                {first: "Barack", last: "Obama", number: 44},
                {first: "George", last: "Bush", number: 43},
                {first: "William", last: "Clinton", number: 42},
                {first: "George H. W.", last: "Bush", number: 41},
                {first: "Ronald", last: "Reagan", number: 40},
                {first: "James", last: "Carter", number: 39},
                {first: "Gerald R.", last: "Ford", number: 38},
                {first: "Richard M.", last: "Nixon", number: 37},
                {first: "Lyndon B.", last: "Johnson", number: 36},
                {first: "John F.", last: "Kennedy", number: 35},
                {first: "Dwight D.", last: "Eisenhower ", number: 34},
            ]);

 

and a datagrid and export button

<s:VGroup width="100%" height="100%"
              horizontalAlign="center">
        <s:DataGrid id="dgPresidents" dataProvider="{sampleData}">
            <s:columns>
                <s:ArrayCollection>
                    <s:GridColumn dataField="first" headerText="First Name"/>
                    <s:GridColumn dataField="last" headerText="Last Name"/>
                    <s:GridColumn dataField="number" headerText="#"/>
                </s:ArrayCollection>
            </s:columns>
        </s:DataGrid>
        <s:Button id="btnExport" label="Export" click="btnExport_clickHandler(event)"/>
    </s:VGroup>

 

Next is the most important part whic is the logic behind the export:

 

protected function btnExport_clickHandler(event:MouseEvent):void
            {
                var fieldList:Array = [];
               
                var csv:CSV = new CSV();
                csv.embededHeader = true;
                csv.header = [];
                for (var i:int = 0; i < dgPresidents.columns.length; i++) {
                    csv.header.push(dgPresidents.columns.getItemAt(i).headerText);
                    fieldList.push(dgPresidents.columns.getItemAt(i).dataField);
                }
               
                for (var j:int = 0; j < dgPresidents.dataProvider.length; j++) {
                    var tempObj:Object = dgPresidents.dataProvider.getItemAt(j);
                    var tempArray:Array = [];
                    for (var k:int = 0; k < fieldList.length; k++) {
                        tempArray.push(tempObj[fieldList[k]]);
                    }
                    csv.addRecordSet(tempArray);
                }
               
                csv.encode();
               
                writeToFile(File.documentsDirectory.resolvePath("testCSVExport.csv"), csv.data);
            }

 

 

The first part of this function pulls our the headers that are used in the datagrid and creates a simple mapping to the dataFields for the objects. 

var fieldList:Array = [];
               
                var csv:CSV = new CSV();
                csv.embededHeader = true;
                csv.header = [];
                for (var i:int = 0; i < dgPresidents.columns.length; i++) {
                    csv.header.push(dgPresidents.columns.getItemAt(i).headerText);
                    fieldList.push(dgPresidents.columns.getItemAt(i).dataField);
                }

 

 

Next iterate through each object in the dataProvider and pull out the data that corresponds with the header.

for (var j:int = 0; j < dgPresidents.dataProvider.length; j++) {
                    var tempObj:Object = dgPresidents.dataProvider.getItemAt(j);
                    var tempArray:Array = [];
                    for (var k:int = 0; k < fieldList.length; k++) {
                        tempArray.push(tempObj[fieldList[k]]);
                    }
                    csv.addRecordSet(tempArray);
                }

 

Finally encode the file and write it out:

csv.encode();
              
                writeToFile(File.documentsDirectory.resolvePath("testCSVExport.csv"), csv.data);

 

You could do this easily all on your own just by writing out a series of lines separated by commas, tabs, or whatever you choose as your delimiter.  But personally I prefer to let the library take care of all the annoying work.


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes