2 Replies Latest reply on Oct 24, 2011 6:41 AM by xandout

    how to export data from datagrid to openoffice excel sheet in flex 4 mxml web application.

    Venkatesan Balasubramanian Level 1

      Hello friends,



      I am having one data grid it having 9 columns and 10 rows, now i want to export this data into to excel,how to creat new excel and export this data into a excel


      to that excel,


      i am using openoffice, as3xls 1.0.0,flashplayer,


      i created excel sheet but it not showing all my records, it showing only one record,that also not in alingnment.


      how to created a new and export data to that openoffice excel file.

      my code is:




      Reportgrid denote the id of the datagrid.




                  private var fields: Array = new Array ();

                  private function generateXLS(e:MouseEvent):void{

                      sheet = new Sheet ();

                      var dataProviderCollection: ArrayCollection = Reportgrid.dataProvider as ArrayCollection;

                      var rowCount: int = dataProviderCollection.length;

                      sheet.resize (rowCount+1,Reportgrid.columnCount);

                      var columns: Array = Reportgrid.columns;

                      var i: int = 0;

                      for each (var field: DataGridColumn in columns)


                          fields.push (field.dataField.toString());

                          sheet.setCell (0, i, field.dataField.toString());




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



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

                              insertRecordInSheet (r +1,sheet,record);


                          var xls:ExcelFile = new ExcelFile ();

                      xls.sheets.addItem (sheet);


                      var bytes:ByteArray = xls.saveToByteArray ();

                      var fr:FileReference = new FileReference ();

                      fr.save(bytes, "Reports.xls");


                      private function insertRecordInSheet (row:int, sheet:Sheet, record:Object): void


                          var colCount: int = Reportgrid.columnCount;

                          for (var c:int; c<colCount; c++)


                                  var i:int = 0;

                                  for each (var field: String in fields)


                                      for each (var value: String in record)


                                          if (record [field].toString()== value)

                                              sheet.setCell (row, i, value);









      Please give any useful suggession or snippet sample.


      Thanks in advance,