flex datagrid 导出csv

public  function exportToCSV(dataGrid:DataGrid):void

            { 

                

                var dataProviderCollection:ArrayCollection = dataGrid.dataProvider as ArrayCollection;

                var rowCount:int = dataProviderCollection.length; 

                var ExcelStr:Array = [];//保存表格所有行

                var columnsFiledText:Array = new Array();//列标题

                var columnsFiledName:Array = new Array();//列数据字段

                for each(var column:GridColumn in dataGrid.columns)

                {

                    //保存Datagrid列标题

                    columnsFiledText.push(column.headerText.toString());

                    //保存DataGrid列数据字段名

                    columnsFiledName.push(column.dataField.toString());

                } 

                //保存标题行

                ExcelStr.push(columnsFiledText.join(",").toString());    

                

                for(var r:int = 0;r<rowCount;r++)

                {

                    var rowData:Object = dataProviderCollection.getItemAt(r);    

                    var csvRow:Array =new Array();//定义一行数据    

                    for each(var col:String in columnsFiledName)

                    {  

                        var text:String;

                        text = rowData[col];

                        }

                        csvRow.push(text);//保存每一行的单元格数据

                    } 

                    //保存一行数据

                    ExcelStr.push(csvRow.join(",").toString());

                }            

                var bytes:ByteArray = new ByteArray();

                var fileReference:FileReference = new FileReference();

                bytes.writeMultiByte(ExcelStr.join("\r\n"),"gb2312");

                fileReference.save(bytes,"test.csv");

                

            }

 

你可能感兴趣的:(datagrid)