Flex DataGrid export to MS Excel question.

VKN9876
VKN9876 used Ask the Experts™
on
I have an AdvancedDatagrid with multirow select enabled.My user want to export the grid data to Excel. My function export all grid data to excel. How do I export only selected rows, if the user select multiple rows. Any help appreciated.

Code attached.
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 str:String = '';
        
            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;
                        trace(data);
                    }
 
                    //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;
            str = header + "\r\n" + data;
            return str;
        }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial