Flex Export Excel specific implementation _flex

Source: Internet
Author: User
Tags rowcount setcell
Need plugin AS3XLS-1.0.1.SWC
Copy Code 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: "4 District", ClassName: "5 scavenger but not"},
{studentid:2,name: "2 Grand", 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: "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 ();
Sheet.resize (Rowcount+1,colcount); Set the scope of a table
var fields:array = new Array ()//To save field
for (var i:int=0; i< colcount;i++)
{
Sheet.setcell (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)
{
The/** loop determines whether the MYDG Column Name field value Record[field] is equal to value **/
if (record[field].tostring () = = value)
/** write to the table **/
Sheet.setcell (Row,i,value);
}
i++;
}
}
}
}
]]>
</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:datagridcolumn headertext= "name" datafield= "name"/>
<mx:datagridcolumn headertext= "Gender" datafield= "gender" width= "/>"
<mx:datagridcolumn headertext= "Birthday" datafield= "Birthday"/>
<mx:datagridcolumn headertext= "class" datafield= "ClassName"/>
</mx:columns>
</mx:DataGrid>
</mx:Panel>
</s:Application>

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.