关于Flex页面Datagrid导出Excel问题
HTML code<?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" minWidth="955" minHeight="600" creationComplete="init()"> <fx:Script> <![CDATA[ import com.as3xls.xls.Cell; import com.as3xls.xls.ExcelFile; import com.as3xls.xls.Sheet; import mx.collections.ArrayCollection; import mx.controls.Alert; import mx.controls.dataGridClasses.DataGridColumn; import mx.formatters.DateFormatter; private var fileReference:FileReference; private var xls:Class; private var sheet:Sheet; [Bindable] private var fields:Array = new Array(); [Bindable] private var ItemDGDataProvider:ArrayCollection = new ArrayCollection([ {name:"Item1",value:"21",qty:"3",cost:"12.21", apples: "GDGZ00000120111129100152#3208939133a582990133a5d861330006#GDGZ000001#11111111111#01#0189F1405A905059393232353352AABBCC#019FF1405A80000009147225C30606A053412006122804000000020407030300000000D8AABBCC#0185F1405801A0B12181AABBCC#019FF1125A80000007565300010D0011324C2006120808001059FFFFFFFFFFFFFFFFFF71AABBCC#018BF11258032D32A229F4822D1002C8AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#0183F1297F1A78AE9FF1295A80000006778164060705B048422006121318000BFD041E0C000000000000DAAABBCC#0182F1295800F4AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#019FF1015A80000009134280100808E0475A2006121508001620040A0003280A000000B8AABBCC#0182F1015800CCAABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#019FF1635A80000009141681020209004B412006121557000C23492D0A03030000000073AABBCC#0182F16358002EAABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#019FF1415A80000009149671030506B04B462006121941000BFA040A00030204000000CCAABBCC#0182F14158000CAABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#019FF1725A80000009128183111205C047582006122809000C0407000003030000000008AABBCC#0182F17258003DAABBCC#019FF1565A80000009197737340605A048432002121302000C0305010103030000000867AABBCC#0183F1567F1878D9AABBCC#0182F156580021AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#019FF1785A80000009199260030105D0475520061219560017670B00C403031E00000083AABBCC#0182F178580043AABBCC#019FF1605A800000091414890E0808D0525020061220630016117350800303000000003FAABBCC#0182F16058002BAABBCC#019FF1005A80000009134484050708C04759200403010900128D050900030300000000A6AABBCC#0182F1005800CBAABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABB"}, ]); private function fileReference_Cancel(event:Event):void { fileReference = null; } private function exportToExcel():void { sheet = new Sheet(); var dataProviderCollection:ArrayCollection = rebateByItemDG.dataProvider as ArrayCollection; var rowCount:int = dataProviderCollection.length; sheet.resize(rowCount + 1,rebateByItemDG.columnCount); var columns:Array = rebateByItemDG.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); /*insert record starting from row no 2 else headers will be overwritten*/ 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,"c_templog.xls"); } private function insertRecordInSheet(row:int,sheet:Sheet,record:Object):void { var colCount:int = rebateByItemDG.columnCount; for(var c:int; c <= 10; c++) { var i:int = 0; var count:int=0; var thesize:int=200; for each(var field:String in fields){ for each (var value:String in record){ if (record[field].toString() == value) { sheet.setCell(row,i,value);//给sheet添加数据 } } i++; } } } ]]> </fx:Script> <mx:VBox width="100%" height="100%"> <mx:Form> <mx:FormItem label="Export Datagrid items to Excel?" fontWeight="bold"> <mx:Form> <mx:HBox width="100%" verticalAlign="middle"> <mx:DataGrid id="rebateByItemDG" includeInLayout="true" visible="true" dataProvider="{ItemDGDataProvider}" width="100%" editable="true"/> <mx:Button label="Export To Excel" click="exportToExcel();"/> </mx:HBox> </mx:Form> </mx:FormItem> </mx:Form> </mx:VBox></s:Application>