flex導(dǎo)出excel具體實(shí)現(xiàn)
更新時(shí)間:2014年01月14日 14:53:19 作者:
flex導(dǎo)出excel的前提是需要插件as3xls-1.0.1.swc,下面為大家介紹下具體的實(shí)現(xiàn)
需要插件 as3xls-1.0.1.swc
<?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:"為001",birthday:"4區(qū)",className:"5清道夫卻無(wú)法"},
{studentID:2,name:"2士大",gender:"1色調(diào)",birthday:"4卻",className:"5為去去"},
{studentID:3,name:"2訪問(wèn)",gender:"1色調(diào)",birthday:"飛4",className:"訪問(wèn)5"},
{studentID:4,name:"訪問(wèn)2",gender:"0色調(diào)",birthday:"4卻",className:"為5"},
{studentID:5,name:"2各位",gender:"0色調(diào)",birthday:"4飛",className:"5為"}];
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); //設(shè)置表格的范圍
var fields:Array = new Array();//用來(lái)保存字段
for(var i:int=0; i< colCount;i++)
{
sheet.setCell(0,i,dg.columns[i].headerText.toString());//表格第0行設(shè)置字段名
fields.push(dg.columns[i].dataField.toString());
}
for(var i:int=0; i< rowCount;i++)
{
var record:Object =dg.dataProvider[i];//獲取某行
insertRecordInSheet(i+1,sheet,record);
}
var excelFile:ExcelFile = new ExcelFile();//新建excel文件
excelFile.sheets.addItem(sheet);//把表格添加入excel文件中
var mbytes:ByteArray = excelFile.saveToByteArray();
var file:FileReference = new FileReference();
file.save(mbytes,"測(cè)試文件.xls"); // 定死文件名
file.addEventListener(Event.COMPLETE, function (){
Alert.show("保存成功");
});
/**回調(diào)函數(shù)**/
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)
{
/**循環(huán)判斷myDg列名域值record[field]與value是否相等**/
if (record[field].toString() == value)
/**寫入表格中**/
sheet.setCell(row,i,value);
}
i++;
}
}
}
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 將非可視元素(例如服務(wù)、值對(duì)象)放在此處 -->
</fx:Declarations>
<mx:Panel>
<mx:Button label="導(dǎo)出" click="onCreate(myDG)"/>
<mx:DataGrid id="myDG" width="100%" rowCount="20" dataProvider="{dp}">
<mx:columns>
<mx:DataGridColumn headerText="學(xué)號(hào)" dataField="studentID"/>
<mx:DataGridColumn headerText="姓名" dataField="name"/>
<mx:DataGridColumn headerText="性別" dataField="gender" width="50"/>
<mx:DataGridColumn headerText="生日" dataField="birthday" />
<mx:DataGridColumn headerText="班級(jí)" dataField="className"/>
</mx:columns>
</mx:DataGrid>
</mx:Panel>
</s:Application>
復(fù)制代碼 代碼如下:
<?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:"為001",birthday:"4區(qū)",className:"5清道夫卻無(wú)法"},
{studentID:2,name:"2士大",gender:"1色調(diào)",birthday:"4卻",className:"5為去去"},
{studentID:3,name:"2訪問(wèn)",gender:"1色調(diào)",birthday:"飛4",className:"訪問(wèn)5"},
{studentID:4,name:"訪問(wèn)2",gender:"0色調(diào)",birthday:"4卻",className:"為5"},
{studentID:5,name:"2各位",gender:"0色調(diào)",birthday:"4飛",className:"5為"}];
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); //設(shè)置表格的范圍
var fields:Array = new Array();//用來(lái)保存字段
for(var i:int=0; i< colCount;i++)
{
sheet.setCell(0,i,dg.columns[i].headerText.toString());//表格第0行設(shè)置字段名
fields.push(dg.columns[i].dataField.toString());
}
for(var i:int=0; i< rowCount;i++)
{
var record:Object =dg.dataProvider[i];//獲取某行
insertRecordInSheet(i+1,sheet,record);
}
var excelFile:ExcelFile = new ExcelFile();//新建excel文件
excelFile.sheets.addItem(sheet);//把表格添加入excel文件中
var mbytes:ByteArray = excelFile.saveToByteArray();
var file:FileReference = new FileReference();
file.save(mbytes,"測(cè)試文件.xls"); // 定死文件名
file.addEventListener(Event.COMPLETE, function (){
Alert.show("保存成功");
});
/**回調(diào)函數(shù)**/
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)
{
/**循環(huán)判斷myDg列名域值record[field]與value是否相等**/
if (record[field].toString() == value)
/**寫入表格中**/
sheet.setCell(row,i,value);
}
i++;
}
}
}
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 將非可視元素(例如服務(wù)、值對(duì)象)放在此處 -->
</fx:Declarations>
<mx:Panel>
<mx:Button label="導(dǎo)出" click="onCreate(myDG)"/>
<mx:DataGrid id="myDG" width="100%" rowCount="20" dataProvider="{dp}">
<mx:columns>
<mx:DataGridColumn headerText="學(xué)號(hào)" dataField="studentID"/>
<mx:DataGridColumn headerText="姓名" dataField="name"/>
<mx:DataGridColumn headerText="性別" dataField="gender" width="50"/>
<mx:DataGridColumn headerText="生日" dataField="birthday" />
<mx:DataGridColumn headerText="班級(jí)" dataField="className"/>
</mx:columns>
</mx:DataGrid>
</mx:Panel>
</s:Application>
相關(guān)文章
flex中使用RadioButtonGroup時(shí)取出所選項(xiàng)的值的方法
flex中的RadioButtonGroup想必大家并不陌生吧,在本文將為大家介紹下在使用RadioButtonGroup時(shí)如何取出所選項(xiàng)的值,感興趣的朋友可以參考下2013-12-12flex通過(guò)java后臺(tái)獲取ip和pcname示例代碼
這篇文章主要介紹了flex通過(guò)java后臺(tái)獲取ip和pcname的具體實(shí)現(xiàn),需要的朋友可以參考下2014-05-05利用R語(yǔ)言解壓與壓縮.tar.gz.zip等格式文件
這篇文章主要為大家介紹了利用R語(yǔ)言怎樣解壓與壓縮.tar.gz.zip等各種壓縮格式文件實(shí)現(xiàn)過(guò)程方法,有需要的朋友可以借鑒參考下希望能夠有所幫助2021-11-11flex實(shí)現(xiàn)DataGrid高亮顯示數(shù)據(jù)功能的解決方案
原生的DataGrid根本無(wú)法達(dá)到所要的效果,目前一般就是來(lái)改寫原生的DataGrid,只需重新寫一個(gè)類來(lái)重寫drawRowBackground方法就可以了2013-10-10Flex讀取txt文件中的內(nèi)容報(bào)錯(cuò)原因分析及解決
這篇文章主要介紹了Flex讀取txt文件中的內(nèi)容報(bào)錯(cuò)原因分析及解決,需要的朋友可以參考下2014-05-05flex4.5中CSS選擇器的應(yīng)用小結(jié)
與HTML相似,F(xiàn)lex允許在MXML標(biāo)簽中通過(guò)CSS樣式來(lái)設(shè)置組件的外觀。到flex4.5后已經(jīng)基本上支持了HTML中的所有CSS的應(yīng)用方式,這里主要來(lái)列舉下flex4.5中CSS選擇器的使用方法2013-04-04Flex 基于數(shù)據(jù)源的Menu Tree實(shí)現(xiàn)代碼
由外部參數(shù)flashvars指定數(shù)據(jù)源的文件位置或render鏈接,在源數(shù)據(jù)上加href和target屬性來(lái)控制打開窗口,可自定義父節(jié)點(diǎn)和子節(jié)點(diǎn)圖標(biāo),不設(shè)置采用系統(tǒng)默認(rèn),感興趣的你可以了解下啊,或許對(duì)你有所幫助2013-01-01Flex4 DataGrid中如何嵌入RadioButton
本文為大家介紹下Flex4 DataGrid中嵌入RadioButton的方法,下面有個(gè)不錯(cuò)的示例,感興趣的朋友可以參考下2013-12-12