加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flex把datagrid的内容导出到Excel

发布时间:2020-12-15 01:09:20 所属栏目:百科 来源:网络整理
导读:?最近在做这个,要下载一个as3xls.swc文件,这个是开源的。 ? ??? 下面介绍下datagrid数据保存到excel文件中。 ?????? ???? ?xml version="1.0" encoding="utf-8"? mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationCom
?最近在做这个,要下载一个as3xls.swc文件,这个是开源的。

?

??? 下面介绍下datagrid数据保存到excel文件中。

??????

???? <?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();">
??? <mx:Script>
??? ??? <![CDATA[
??? ??? ??? import com.as3xls.xls.Cell;
??? ??? ??? import mx.collections.ArrayCollection;
??? ??? ???
??? ??? ??? import com.as3xls.xls.Sheet;
??? ??? ??? import com.as3xls.xls.ExcelFile;
??? ??? ??? import mx.controls.Alert;
??? ??? ??? private var fileReference:FileReference;
??? ??? ??? private var xls:Class;
??? ??? ??? private var sheet:Sheet;
??? ??? ???
??? ??? ??? [Bindable]
??? ??? ??? private var dg:Array;
??? ??? ???
??? ??? ??? [Bindable]
??? ??? ??? private var fields:Array = new Array();
??? ??? ??? private function init():void
??? ??? ??? {
??? ??? ??? ??? dg=new Array();
??? ??? ??? ??? for(var i:int=0;i<4;i++)
??? ??? ??? ??? {
??? ??? ??? ??? ??? //ItemName/ItemCost/ItemQty/ItemPrice
??? ??? ??? ??? ??? var tempArray:Array = new Array();
??? ??? ??? ??? ??? tempArray.ItemName="ItemName^^"+i;
??? ??? ??? ??? ??? tempArray.ItemCost="ItemCost^^"+i;
??? ??? ??? ??? ??? tempArray.ItemQty="ItemQty^^"+i;
??? ??? ??? ??? ??? tempArray.ItemPrice="ItemPrice^^"+i;
??? ??? ??? ??? ??? dg.push(tempArray);
??? ??? ??? ??? }
??? ??? ??? ???
??? ??? ??? }
??? ??? ???
??? ??? ??? private function exportToExcel():void
??? ??? ??? {
??? ??? ??? ??? sheet = new Sheet();
??? ??? ??? ??? var dataProviderCollection:ArrayCollection =myDg.dataProvider as ArrayCollection;
??? ??? ??? ??? var rowCount:int =? dataProviderCollection.length;
??? ??? ??? ??? sheet.resize(rowCount+1,myDg.columnCount);
??? ??? ??? ??? var columns:Array = myDg.columns;
??? ??? ??? ???
??? ??? ??? ??? var i:int = 0;
??? ??? ??? ??? for each (var field:DataGridColumn in columns)
??? ??? ??? ??? {
??? ??? ??? ??? ??? fields.push(field.dataField.toString());
??? ??? ??? ??? ??? sheet.setCell(0,i,field.dataField.toString());
??? ??? ??? ??? ??? i++;
??? ??? ??? ??? }
??? ??? ??? ???
??? ??? ??? ??? 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,"SampleExport.xls");
??? ??? ??? }
??? ??? ??? private function insertRecordInSheet(row:int,sheet:Sheet,record:Object):void
??? ??? ??? {
??? ??? ??? ??? var colCount:int = myDg.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,value);
??? ??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? ??? i++;
??? ??? ??? ??? ??? }
??? ??? ??? ??? }
??? ??? ??? }
??? ??? ]]>
??? </mx:Script>
??? <mx:DataGrid id="myDg" x="78" y="55" width="533" height="157"? dataProvider="{dg}">
??? ??? <mx:columns>
??? ??? ??? <mx:DataGridColumn headerText="ItemName" dataField="ItemName"/>
??? ??? ??? <mx:DataGridColumn headerText="ItemCost" dataField="ItemCost"/>
??? ??? ??? <mx:DataGridColumn headerText="ItemQty" dataField="ItemQty"/>
??? ??? ??? <mx:DataGridColumn headerText="ItemPrice" dataField="ItemPrice"/>
??? ??? </mx:columns>
??? </mx:DataGrid>
??? <mx:Button id="myBtn" x="619" y="113" label="exporttoexcel" click="exportToExcel();"/>??
</mx:Application>

?

FileReference是flash10.0.0版本所有的。所以你要使用此类,必须设置你的开发环境。具体右键-->properties-->Flex compiler里面设置版本。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读