as3xls导出Excel和解决导出中文乱码问题(原创)2010-11-24 13:37
as3xls导出Excel,解决导出Excel中文乱码问题
<?xml version="1.0" encoding="utf-8"?>
<mx:Module xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
layout="vertical" width="100%" height="100%"
creationComplete="initData();">
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import com.as3xls.xls.Cell;
import com.as3xls.xls.ExcelFile;
import com.as3xls.xls.Sheet;
import mx.collections.ArrayCollection;
import mx.collections.XMLListCollection;
import mx.controls.Alert;
import mx.controls.dataGridClasses.DataGridColumn;
import mx.managers.PopUpManager;
private var xls:Class;
private var sheet:Sheet;
[Bindable]
private var fields:Array = new Array();
//初始化数据
private function initData():void{
var xml:XML=new XML(<data>
<item name="qiyy" sex="男" age="23" phone="13032135708"/>
<item name="zhangmm" sex="man" age="23" phone="13032135708"/>
<item name="yaox" sex="man" age="23" phone="13032135708"/>
</data>);
rebateByItemDG.dataProvider=xml.item;
}
private function setExportExcel():void{
//这里要判断是XML格式的还是ArrayCollection格式的
var arrC:ArrayCollection=new ArrayCollection();
for each(var x:XML in rebateByItemDG.dataProvider){
var obj:Object=new Object();
obj.name=x.@name;
obj.sex=x.@sex;
obj.age=x.@age;
obj.phone=x.@phone;
arrC.addItem(obj);
}
//导出Excel
exportToExcel(arrC);
}
//导出Excel
private function exportToExcel(arrC:ArrayCollection):void{
var dataProviderCollection:ArrayCollection=new ArrayCollection();
dataProviderCollection=arrC;
sheet = new Sheet();
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,columns[i].headerText);
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 = rebateByItemDG.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){
//xml数据
if (record[field.split("@")[1]].toString() == value) {
sheet.setCell(row,i,value);
}
}
i++;
}
}
}
//关闭窗体
private function closewindow():void{
PopUpManager.removePopUp(this);
}
]]>
</fx:Script>
<s:VGroup>
<mx:DataGrid id="rebateByItemDG" includeInLayout="true" visible="true" width="100%" editable="true">
<mx:columns>
<mx:DataGridColumn headerText="姓名" dataField="@name"/>
<mx:DataGridColumn headerText="性别" dataField="@sex"/>
<mx:DataGridColumn headerText="年龄" dataField="@age"/>
<mx:DataGridColumn headerText="电话" dataField="@phone"/>
</mx:columns>
</mx:DataGrid>
<mx:Button label="Export To Excel" click="setExportExcel();"/>
<mx:Button label="Close Window" click="closewindow();"/>
</s:VGroup>
</mx:Module>
支持中文版的as3xls下载地址:as3xls.swc(CSDN下载要注册的)
1 楼 晓晨Happy 2012-06-03 受教了 ,多谢楼主。