Flex界面DateGrid数据导出Excel表格
1.FileReference的save方法在Flash10才支持,在Flex工程上右键,属性,Flex Conpiler中把Require Flash Player version:填为10.0.0。 Test.mxml: <?xml version="1.0" encoding="utf-8"?> ? <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();"> ??? ?<mx:Script> ? ?? ??? ?<![CDATA[ ? ?? ??? ??? ? ?? ??? ??? ?[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<5;i++) ? ?? ??? ??? ??? ?{ ? ?? ??? ??? ??? ??? ?//ItemName/ItemCost/ItemQty/ItemPrice ? ?? ??? ??? ??? ??? ?var tempArray:Array = new Array(); ? ?? ??? ??? ??? ??? ?tempArray.ItemName="测试"+i; ? ?? ??? ??? ??? ??? ?tempArray.ItemCost="调试"+i; ? ?? ??? ??? ??? ??? ?tempArray.ItemQty="ItemQty^^"+i; ? ?? ??? ??? ??? ??? ?tempArray.ItemPrice="ItemPrice^^"+i; ? ?? ??? ??? ??? ??? ?dg.push(tempArray); ? ?? ??? ??? ??? ?} ? ?? ??? ??? ??? ? ?? ??? ??? ?} ? ?? ??? ??? ?public function exportTo():void ? ?? ??? ??? ?{ ? ?? ??? ??? ??? ?///调用Util类的静态方法 ? ?? ??? ??? ??? ?Util.exportToExcel(myDg); ?? ??? ??? ?} ? ?? ??? ?]]> ? ?? ?</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="exportTo();"/>??? ? </mx:Application>? ================================ as工具类:Util.as package { ? ?? ?import com.as3xls.xls.Cell; ? ?? ?import mx.collections.ArrayCollection; ? ?? ?import flash.events.*; ? ?? ?import flash.net.FileReference; ? ?? ?import com.as3xls.xls.Sheet; ? ?? ?import com.as3xls.xls.ExcelFile; ? ?? ?import mx.controls.Alert; ? ?? ?import mx.controls.DataGrid; ? ?? ?import mx.controls.dataGridClasses.DataGridColumn; ? ?? ?import flash.utils.ByteArray; ? ?? ?import mx.controls.Alert; ? ?? ?public class Util ? ?? ?{ ? ?? ??? ?public function Util() ? ?? ??? ?{ ? ?? ??? ?} ? ?? ??? ?/**导出Excel表格函数,参数为DataGrid**/ ? ?? ??? ?public static function exportToExcel(myDg:DataGrid):void ? ?? ??? ?{?? ? ?? ??? ??? ? ?? ??? ??? ?var fields:Array = new Array();? ? ?? ??? ??? ?/**生成表对象sheet**/ ? ?? ??? ??? ?var sheet:Sheet= new Sheet(); ? ?? ??? ??? ? ?? ??? ??? ?var dataProviderCollection:ArrayCollection =myDg.dataProvider as ArrayCollection; ? ?? ??? ??? ?/**获得表格的行数**/ ? ?? ??? ??? ?var rowCount:int =? dataProviderCollection.length; ? ?? ??? ??? ?/**设置表格的行数(rowCount+1),列数(myDg.columnCount)**/ ? ?? ??? ??? ?sheet.resize(rowCount+1,myDg.columnCount); ? ?? ??? ??? ?/**获得DateGrid列的内容**/ ? ?? ??? ??? ?var columns:Array = myDg.columns;? ? ?? ??? ??? ?/**循环设置列名的值**/ ? ?? ??? ??? ?var i:int = 0;? ? ?? ??? ??? ?for each (var field:DataGridColumn in columns) ? ?? ??? ??? ?{?? ? ?? ??? ??? ??? ?fields.push(field.dataField.toString());? ? ?? ??? ??? ??? ?/**第一行的值,取值为myDg的headerText**/ ? ?? ??? ??? ??? ?sheet.setCell(0,i,field.headerText.toString());? ? ?? ??? ??? ??? ?i++;? ? ?? ??? ??? ?} ? ?? ??? ??? ?/**循环设置行的值**/ ? ?? ??? ??? ?for(var r:int=0;r<rowCount;r++) ? ?? ??? ??? ?{ ? ?? ??? ??? ??? ?/**获得dataProviderCollection的每行Item的对象**/ ? ?? ??? ??? ??? ?var record:Object =dataProviderCollection.getItemAt(r); ? ?? ??? ??? ??? ?/**调用回调函数写入sheet**/ ? ?? ??? ??? ??? ?insertRecordInSheet(r+1,sheet,record); ? ?? ??? ??? ?} ? ?? ??? ??? ?/**生成Excel文件**/ ? ?? ??? ??? ?var xls:ExcelFile = new ExcelFile(); ? ?? ??? ??? ?/**将sheet写入Excel文件中**/ ? ?? ??? ??? ?xls.sheets.addItem(sheet); ? ?? ??? ??? ?/**将xls对象转换为ByteArray流对象**/ ? ?? ??? ??? ?var bytes: ByteArray = xls.saveToByteArray(); ? ?? ??? ??? ?/**生成新文件域**/ ? ?? ??? ??? ?var fr:FileReference = new FileReference(); ? ?? ??? ??? ?/**将bytes流对象保存到文件域**/ ? ?? ??? ??? ?fr.save(bytes,"SampleExport.xls"); ? ?? ??? ??? ? ?? ??? ??? ?/**回调函数**/ ? ?? ??? ??? ?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) ? ?? ??? ??? ??? ??? ??? ?{? ? ?? ??? ??? ??? ??? ??? ??? ?/**循环判断myDg列名域值record[field]与value是否相等**/ ? ?? ??? ??? ??? ??? ??? ??? ?if (record[field].toString() == value)? ? ?? ??? ??? ??? ??? ??? ??? ??? ?/**写入表格中**/ ? ?? ??? ??? ??? ??? ??? ??? ??? ?sheet.setCell(row,value);? ? ?? ??? ??? ??? ??? ??? ?}? ? ?? ??? ??? ??? ??? ??? ?i++;? ? ?? ??? ??? ??? ??? ?}? ? ?? ??? ??? ??? ?} ? ?? ??? ??? ?} ? ?? ??? ?} ? ?? ?} ? }? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |