关闭

FileReference as3xls

标签: functionapplicationbuttonuploadobjectstring
795人阅读 评论(0) 收藏 举报
分类:

load/save只能在flex4中出现,complie version 10以上,flash player 10以上

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" horizontalAlign="center" verticalAlign="middle" applicationComplete="initApp()" width="502" height="436">
	<mx:Script>
		<![CDATA[
			import mx.controls.Alert;
			private var fileRef:FileReference=new FileReference();
			private function initApp():void{
				fileRef.addEventListener(Event.SELECT, onFileSelected); 
				fileRef.addEventListener(Event.CANCEL, onCancel);
				fileRef.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
			}
			private function onFileSelected(e:Event):void{
				fileRef.addEventListener(ProgressEvent.PROGRESS,onProgress);
				fileRef.addEventListener(Event.COMPLETE, onComplete);
				fileRef.load(); 方法可以将数据从本地文件加载到内存中
			}
			private function onProgress(e:ProgressEvent):void{
				progress.text="Loaded " + e.bytesLoaded + " of " + e.bytesTotal + " bytes.";
			}
			private function onComplete(e:Event):void{
				img.source=fileRef.data;
			}
			private function onCancel(e:Event):void{
				
			}
			private function onIOError(e:IOErrorEvent):void{
				progress.text="There was an IO Error";
			}
			private function onBrowse():void{
				var typeFilter:FileFilter=new FileFilter("Images(*.jpg,*.jpeg,*.gif,*.png)","*.jpg;*.jpeg;*.gif;*.png")
				fileRef.browse([typeFilter]);
			}
			private function onUpload():void{
				fileRef.upload(new URLRequest("http://localhost/upload.php"));
			}
			private function onSave():void{				
				fileRef.save("123","1.txt");
			}
		]]>
	</mx:Script>
	<mx:Panel title="Upload" width="400" height="349" id="panel" layout="absolute">
		<mx:Button label="Browse..." click="onBrowse()" fontSize="12" x="70" width="90" y="10"/>
		<mx:Button label="Upload" click="onUpload()" x="180" y="12" width="87" fontSize="12"/>
		<mx:Button label="Save" click="onSave()" fontSize="12" x="70" width="90" y="10"/>
		<mx:Label id="progress" x="47" y="271" width="258" fontSize="12" height="28"/>
		<mx:Image id="img" x="70" y="62" width="197" height="183"/>
	</mx:Panel>
</mx:Application>

解决了中文乱码的as3xlsUTF8.swc包

var sheet:Sheet = new Sheet();
private function export2ExcelHandel(e:MouseEvent):void
{
    //在前台生成Excel
    var excelFile:ExcelFile=new ExcelFile();
    var arr:ArrayCollection=myTable.dataProvider as ArrayCollection;
    for(var i:int=0; i<arr.length; i++)
    {
        var obj:Object=arr[i];
        var j:int=0;
        for(var key:String in obj)
        {
            excelFile.sheets.addItem(generateSheet(0,j,key));	
            excelFile.sheets.addItem(generateSheet(i,j,obj[key]));
            j++;
        }
    }       
    var mbytes:ByteArray=excelFile.saveToByteArray();
    var file:FileReference=new FileReference();
    file.save(mbytes,"测试文件.xls"); 
}
			
function generateSheet(i:int,j:int,o:Object):Sheet{
    if(!sheet){    
        sheet = new Sheet();    
        sheet.resize(10,10);    
    }    
    sheet.setCell(i, j, String(o));    
    return sheet;    
}    




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:187847次
    • 积分:3084
    • 等级:
    • 排名:第11198名
    • 原创:123篇
    • 转载:36篇
    • 译文:0篇
    • 评论:16条
    文章分类
    最新评论