返回顶部

收藏

Flex导入Excel文件示例

更多

1.SDK3.0

2.导入文件用FileFilter过滤。由于是示例,只写了xls

3.导入文件解析在Java端完成,前端在导入之后,从session中得到导入文件的解析结果,然后再调用Java方法销毁session

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="<a href="http://www.adobe.com/2006/mxml">http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script>
        <![CDATA[
            import mx.rpc.remoting.mxml.RemoteObject;
            import mx.collections.ArrayCollection;
            import mx.controls.Alert;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
            private var fileRefer:FileReference = new FileReference()
            //浏览按钮,选择文件
            private function importClick(event:MouseEvent):void {
                //过滤文件
                var fileType:FileFilter = new FileFilter("Excel","*.xls");
                var fileTypes:Array = new  Array(fileType);
                //导入
                fileRefer.addEventListener(Event.SELECT,parseExcel);
                fileRefer.browse(fileTypes);
            }

            private function parseExcel(evt:Event):void {
                //调用Servlet
                var url:URLRequest=new  URLRequest("/web/ExcelUp?loadType=parseExcel");
                fileRefer.upload(url);
                //定时器  获取session
                setTimeout(function():void{
                        readfromflex();
                    },3000);
            }
            //Remote对象
            private var ro:RemoteObject=new RemoteObject("operdisp_display_excelImportService");
            //获取session
            private function readfromflex():void {
                ro.showBusyCursor = true;
                ro.getExcelSession();
                ro.getExcelSession.addEventListener(ResultEvent.RESULT, getExcelResult);
                ro.getExcelSession.addEventListener(FaultEvent.FAULT, setFault); 
            }
            //回调
            private function getExcelResult(e:ResultEvent):void{
                if (e.result != null) {
                    var arr:ArrayCollection = e.result as ArrayCollection;
                    //打印导入的excel中的数据
                    log.debug(arr);
                    //销毁session
                    distorySession();
                } else {
                    readfromflex();
                }
            }
            //销毁session
            private function distorySession():void {
                ro.showBusyCursor = true;
                ro.distorySession();
                ro.distorySession.addEventListener(ResultEvent.RESULT, distorySuccess);
                ro.distorySession.addEventListener(FaultEvent.FAULT, setFault);
            }

            private function distorySuccess(e:ResultEvent):void {

            }
            private function setFault(e:FaultEvent):void{
                Alert.yesLabel="确认";
            }
        ]]>
    </mx:Script>
    <mx:Button label="导入" click="importClick(event)">
    </mx:Button>
</mx:Application>
//该片段来自于http://outofmemory.cn

标签:flash,ActionScript,Excel

收藏

0人收藏

支持

0

反对

0

发表评论