flex导出excel具体实现
网络编程 2021-07-05 11:32www.168986.cn编程入门
flex导出excel的前提是需要插件as3xls-1.0.1.swc,狼蚁网站SEO优化为大家介绍下具体的实现
需要插件 as3xls-1.0.1.swc
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe./mxml/2009"
xmlns:s="library://ns.adobe./flex/spark"
xmlns:mx="library://ns.adobe./flex/mx">
<fx:Script>
<![CDATA[
import .as3xls.xls.ExcelFile;
import .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区",className:"5清道夫却无法"},
{studentID:2,name:"2士大",gender:"1色调",birthday:"4却",className:"5为去去"},
{studentID:3,name:"2访问",gender:"1色调",birthday:"飞4",className:"访问5"},
{studentID:4,name:"访问2",gender:"0色调",birthday:"4却",className:"为5"},
{studentID:5,name:"2各位",gender:"0色调",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); //设置表格的范围
var fields:Array = new Array();//用来保存字段
for(var i:int=0; i< colCount;i++)
{
sheet.setCell(0,i,dg.columns[i].headerText.toString());//表格第0行设置字段名
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,"测试文件.xls"); // 定死文件名
file.addEventListener(Event.COMPLETE, function (){
Alert.show("保存成功");
});
/回调函数/
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)
{
/循环判断myDg列名域值record[field]与value是否相等/
if (record[field].toString() == value)
/写入表格中/
sheet.setCell(row,i,value);
}
i++;
}
}
}
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<mx:Panel>
<mx:Button label="导出" click="onCreate(myDG)"/>
<mx:DataGrid id="myDG" width="100%" rowCount="20" dataProvider="{dp}">
<mx:columns>
<mx:DataGridColumn headerText="学号" dataField="studentID"/>
<mx:DataGridColumn headerText="姓名" dataField="name"/>
<mx:DataGridColumn headerText="性别" dataField="gender" width="50"/>
<mx:DataGridColumn headerText="生日" dataField="birthday" />
<mx:DataGridColumn headerText="班级" dataField="className"/>
</mx:columns>
</mx:DataGrid>
</mx:Panel>
</s:Application>
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe./mxml/2009"
xmlns:s="library://ns.adobe./flex/spark"
xmlns:mx="library://ns.adobe./flex/mx">
<fx:Script>
<![CDATA[
import .as3xls.xls.ExcelFile;
import .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区",className:"5清道夫却无法"},
{studentID:2,name:"2士大",gender:"1色调",birthday:"4却",className:"5为去去"},
{studentID:3,name:"2访问",gender:"1色调",birthday:"飞4",className:"访问5"},
{studentID:4,name:"访问2",gender:"0色调",birthday:"4却",className:"为5"},
{studentID:5,name:"2各位",gender:"0色调",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); //设置表格的范围
var fields:Array = new Array();//用来保存字段
for(var i:int=0; i< colCount;i++)
{
sheet.setCell(0,i,dg.columns[i].headerText.toString());//表格第0行设置字段名
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,"测试文件.xls"); // 定死文件名
file.addEventListener(Event.COMPLETE, function (){
Alert.show("保存成功");
});
/回调函数/
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)
{
/循环判断myDg列名域值record[field]与value是否相等/
if (record[field].toString() == value)
/写入表格中/
sheet.setCell(row,i,value);
}
i++;
}
}
}
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<mx:Panel>
<mx:Button label="导出" click="onCreate(myDG)"/>
<mx:DataGrid id="myDG" width="100%" rowCount="20" dataProvider="{dp}">
<mx:columns>
<mx:DataGridColumn headerText="学号" dataField="studentID"/>
<mx:DataGridColumn headerText="姓名" dataField="name"/>
<mx:DataGridColumn headerText="性别" dataField="gender" width="50"/>
<mx:DataGridColumn headerText="生日" dataField="birthday" />
<mx:DataGridColumn headerText="班级" dataField="className"/>
</mx:columns>
</mx:DataGrid>
</mx:Panel>
</s:Application>
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程