4 Replies Latest reply on Jan 31, 2007 12:00 AM by Like2Flex

    Export DataGrid to Excel

    Like2Flex
      Hello,

      I populated a datagrid using a query. Now I want to export the content of the datagrid to Excel.

      Is this possible?
      I don't want to use the query to Export to Excel.
      I want to Export from the datagrid itself.

      Anyone has an idea how to export my datagrid to Excel in flex?

      Thx
        • 1. Re: Export DataGrid to Excel
          rkschulz Level 1
          There was a lengthy discussion about this a while back. Here is what it boils down to.

          1) You cannot "download" files straight out of the Flex client. Downloads are only supported as "pass throughs" streams from the server and your flex client can't fiddle with it. I guess that's a good thing from a firewall filtering point of view.

          2) There may be a path using activeX and / or Javascript but that sounds messy so I did not chase it.

          3) Using the clipboard works pretty well for me and that's what I do. Use System.setClipboard. Excel wants slash t as column separators and slash n for new lines. I dump it to the clipboard and then pop an alert telling the user to paste it into Excel.

          Hope this helps.

          Robert.



          • 2. Re: Export DataGrid to Excel
            Like2Flex Level 1
            Robert,

            Can you give me a sample to see how it works.

            Thx
            • 3. Re: Export DataGrid to Excel
              rkschulz Level 1
              Code is below. I simplified it a bit for this post, I hope it still works ;-)

              Couple of assumptions
              1) My dataproviders are XMLListCollections - you have to adapt that if you are using something else
              2) I set a dataField on the datagrid columns for the XML attribute I want to read

              I am sure there are better / more generic ways of doing this, but this works for me right now and I blew a lot of time on this when I first came across the problem. If you find a better solution let me know ;-)


              public static function copyGridData(srcDataGrid:DataGrid):void {
              var outputA:Array = new Array();

              // header
              var headerA:Array = new Array();
              var columns:Array = srcDataGrid.columns;
              for each (var col:DataGridColumn in columns ) {
              headerA.push(col.headerText);
              }
              outputA.push(headerA.join("\t"));

              // data
              var data:XMLListCollection = XMLListCollection(srcDataGrid.dataProvider);
              for each (var r:XML in data) {
              var rowA:Array = new Array();
              for each (var col2:DataGridColumn in columns) {
              if (col2.dataField != null) {
              rowA.push(r.attribute(col2.dataField.substr(1,col2.dataField.length-1)));
              } else {
              rowA.push("");
              }
              }
              }
              outputA.push(rowA.join("\t"));
              }

              // copy onto the clipboard
              System.setClipboard(outputA.join("\n"));
              Alert.show("Data copied to the clipboard.\nOpen target application (e.g. Excel) and paste data.", "Data Copy");
              }
              • 4. Re: Export DataGrid to Excel
                Like2Flex Level 1
                I cant make the code work.

                I am getting errors in flex :
                "Access of undefined propertyA"

                I copied, pasted the code as it is, I only changed the gridname from "srcDataGrid" to "mygrid"

                Pls help