flex導(dǎo)出excel具體實(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清道夫卻無法"},
{studentID:2,name:"2士大",gender:"1色調(diào)",birthday:"4卻",className:"5為去去"},
{studentID:3,name:"2訪問",gender:"1色調(diào)",birthday:"飛4",className:"訪問5"},
{studentID:4,name:"訪問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();//用來保存字段
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>
上一篇:flex自定義按鈕皮膚示例附圖
欄 目:Flex
下一篇:Flex中TitleWindow傳值思路及實(shí)現(xiàn)
本文標(biāo)題:flex導(dǎo)出excel具體實(shí)現(xiàn)
本文地址:http://mengdiqiu.com.cn/a1/Flex/11583.html
您可能感興趣的文章
- 01-11flex調(diào)用webservice中的自定義類的方法
- 01-11Flex實(shí)現(xiàn)的上傳攝像頭拍照并將UI保存為圖片
- 01-11Flex字體加粗問題只能對(duì)英文的字體加粗
- 01-11flex利用webservice上傳照片實(shí)現(xiàn)代碼
- 01-11Flex控制彈出窗口拖動(dòng)范圍示例代碼
- 01-11flex內(nèi)嵌html網(wǎng)頁示例代碼
- 01-11Flex中在Tree綁定數(shù)據(jù)后自動(dòng)展開樹節(jié)點(diǎn)的方法
- 01-11Flex彈出窗口請(qǐng)求Action函數(shù)示例
- 01-11Flex中通過RadioButton進(jìn)行切換示例代碼
- 01-11Flex中TabNavigator設(shè)置Tabs樣式思路及源碼


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-11flex調(diào)用webservice中的自定義類的方法
- 01-11Flex實(shí)現(xiàn)的上傳攝像頭拍照并將UI保存
- 01-11datagrid不可編輯行有關(guān)問題的控制方法
- 01-11Flex控制彈出窗口拖動(dòng)范圍示例代碼
- 01-11flex利用webservice上傳照片實(shí)現(xiàn)代碼
- 01-11Flex字體加粗問題只能對(duì)英文的字體加
- 01-11Flex中在Tree綁定數(shù)據(jù)后自動(dòng)展開樹節(jié)點(diǎn)
- 01-11flex內(nèi)嵌html網(wǎng)頁示例代碼
- 01-11Flex中通過RadioButton進(jìn)行切換示例代碼
- 01-11Flex彈出窗口請(qǐng)求Action函數(shù)示例
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改