flex datagrid 数据保存到excel以及从excel读取数据
??? 最近在做这个,要下载一个as3xls.swc文件,这个是开源的。或者直接下载源代码。在例子中有源代码,swc也在下载文件中,可以下载并查看。例子中是别人写的,用AIR写的,主要是读取Excel文件内容,后台打印出来的。(我也是研究别人的东西呵呵)
?
??? 下面介绍下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,i,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里面设置版本。
1 楼 kimmking 2010-08-29 as3xls 有合并单元格的处理不了。 2 楼 roger_588 2010-08-31 这个没研究过。谁知道可以说说。 3 楼 iMzw 2010-10-01 as3xls类库没有太实际应用的价值 4 楼 iMzw 2010-10-01 如果要导出excel建议服务器处理 5 楼 iMzw 2010-10-01 如果要全功能的excel(多sheet,各种函数等等),必须实现 BIFF version 8 (http://sc.openoffice.org/excelfileformat.pdf) 6 楼 roger_588 2010-10-08 iMzw 写道如果要导出excel建议服务器处理