- <?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://www.codesnippet.cn/detail/030420132693.html
来源: http://www.codesnippet.cn/detail/030420132693.html