Flex Export Excel Concrete implementation

Source: Internet
Author: User
Tags rowcount xmlns

 flex export Excel is a prerequisite for plug-in AS3XLS-1.0.1.SWC, the following for you to introduce the implementation of the specific

Need plugin as3xls-1.0.1.swc  code as follows: <?xml version= "1.0" encoding= "Utf-8"?>  <s:application xmlns:fx= "http ://ns.adobe.com/mxml/2009 "  xmlns:s=" Library://ns.adobe.com/flex/spark "  xmlns:mx=" library:// Ns.adobe.com/flex/mx ">  <fx:Script>  <! [cdata[  Import com.as3xls.xls.excelfile;  Import com.as3xls.xls.sheet;    Import mx.collections.arraycollection;  Import mx.controls.alert;  Import mx.controls.checkbox;    [ bindable]  private var Dp:array = [  {studentid:1,name: "2ssdl", Gender: "For 001", Birthday: "Zone 4", ClassName: " 5 scavengers can not "},  {studentid:2,name: 2", Gender: "1 tones", Birthday: "4 but", ClassName: "5 for Go"},  {studentid:3,name: " 2 Access ", Gender:" 1 tones ", Birthday:" Fly 4 ", ClassName:" Access 5 "},  {studentid:4,name:" Access 2 ", Gender:" 0 tones ", Birthday:" 4 but ", ClassName: "for 5"},  {studentid:5,name: "2 Members", Gender: "0 tones", Birthday: "4 Fly", ClassName: "5 for"}];    private function OnCreate (dg:datagrid):void  {  var RowcoUnt:int = dg.dataprovider.length;  var colcount:int = dg.columncount;  var sheet:sheet = new sheet ();  shee T.resize (Rowcount+1,colcount); Set the scope of the table   var fields:array = new Array ()//To save fields   for (var i:int=0 i< colcount;i++)   {  sheet.se Tcell (0,i,dg.columns[i].headertext.tostring ())//table line No. 0 Set field name   Fields.push (dg.columns[i].datafield.tostring ( ); }  for (var i:int=0 i< rowcount;i++)   {  var record:object =dg.dataprovider[i];//get a row   Insertrecordinsheet (I+1,sheet,record); }  var excelfile:excelfile = new Excelfile (); ExcelFile.sheets.addItem (sheet)//Add table to Excel file   var mbytes:bytearray = Excelfile.savetobytearray ();  var file:filereference = new Filereference ();  file.save (MBytes, "test file. xls"); Set dead file name   File.addeventlistener (event.complete, function () {  alert.show ("Save Success"); }); /** callback function * * /  function Insertrecordinsheet (row:int,sheet:sheet,record:object):void  {  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)   { /** loop to determine MYDG column Name field value Record[field] is equal to value **/   if (record[field].tostring () = = value)  /** write to Table **/  Sheet.setcell (row,i,value); }  nbsp } } } } ]]>  </fx:Script>  <fx:Declarations>  <!-- Place non-visual elements (such as services, value objects) here-->  </fx:Declarations>  <mx:Panel>  <mx:button label= "Export" click= "OnCreate (MYDG)"/>  <mx:datagrid id= "MYDG" width= "100%" rowcount= "dataprovider=" "{DP}" >   <mx:columns>  <mx:datagridcolumn headertext= "School Number" datafield= "StudentID"/>  <mx:d Atagridcolumn headertext= "name" datafield= "name"/>  <mx:datagridcolumn headertext= "Sex" dataField= "gender" Width= "/> " <mx:datagridcolumn headertext= "Birthday" datafield= "Birthday"/>  <mx:datagridcolumn headertext= "class" datafield= "ClassName"/>  </mx:columns>  </mx :D atagrid>  </mx:Panel>  </s:Application>   
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.