加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flex读取Excel

发布时间:2020-12-15 03:55:05 所属栏目:百科 来源:网络整理
导读:MXML代码: mx:DataGrid id="dg"/mx:Button label="上传导入文件" click="upload()"/ Actionscript代码: /** * 点击文件选择弹出框 * */private function upload():void{file = new FileReference();file.addEventListener(Event.SELECT,file_select);file.




MXML代码:

		<mx:DataGrid id="dg"/>
		<mx:Button label="上传导入文件" click="upload()"/>  

Actionscript代码:

			/**
			 * 点击文件选择弹出框
			 * */
			private function upload():void{
				file = new FileReference();
				file.addEventListener(Event.SELECT,file_select);
				file.addEventListener(Event.COMPLETE,file_complete); 
				file.addEventListener(ProgressEvent.PROGRESS,file_progress); 
				file.browse();
			}
			
			/**
			 * 选择了excel之后
			 * */
			private function file_select (e:Event):void { 
				if (file.size > 0) { 
					file.load();
				} 
			} 
			

			/**
			 * excel加载完毕
			 * */
			private function file_complete (e:Event):void { 
				xls = new ExcelFile();
				try{
					xls.loadFromByteArray(e.target.data);
				}catch(eofe:EOFError){
					trace(eofe);
				}
				var sheet:Sheet = xls.sheets[0];
				//sheet.rows
				dp = sheet.values;
				dp.removeItemAt(sheet.rows);
				dg.dataProvider = dp;
			} 
			
			/**
			 * 进度条控制
			 * */
			private function file_progress (e:ProgressEvent):void { 
				progressBar.label = Math.round(100 * 
					e.bytesLoaded / e.bytesTotal) + "%";
				progressBar.setProgress(Math.round(100 * 
					e.bytesLoaded / e.bytesTotal),100);
			} 



运行截图:







全部代码:


<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
					   xmlns:s="library://ns.adobe.com/flex/spark" 
					   xmlns:mx="library://ns.adobe.com/flex/mx" 
					  >
	
	<fx:Declarations>
		<!-- Place non-visual elements (e.g.,services,value objects) here -->
	</fx:Declarations>
	
	<fx:Script>
		<![CDATA[
			import com.as3xls.xls.ExcelFile;
			import com.as3xls.xls.Sheet;
			
			import mx.collections.ArrayCollection;
			import mx.controls.Alert;
			import mx.controls.Image;
			import mx.core.IVisualElement;
			
			private var file:FileReference; 
			private var xls:ExcelFile;
			
			var dp:ArrayCollection = new ArrayCollection;
			
			/**
			 * 点击文件选择弹出框
			 * */
			private function upload():void{
				file = new FileReference();
				file.addEventListener(Event.SELECT,100);
			} 
			
			/**
			 * 获取表格数据
			 * */
			protected function gainDgData(event:MouseEvent):void
			{
				var s:String = "";
				var shuju:ArrayCollection = dg.dataProvider as ArrayCollection;
				//行数:shuju.source.length-1 范围:0 到 shuju.source.length-2
				for(var i:uint = 0 ; i < shuju.source.length-1 ; i++){ //一行一行(一条记录一条记录)的遍历
					//每一行的列数为shuju[i]
					//s += shuju[i][0].value; 
				}
				//var t1 = shuju.source.length;
			}
			
		]]>
	</fx:Script>
	
	<mx:Panel width="800" height="600" layout="vertical" title="上传示例" 
			  paddingLeft="20" paddingBottom="20" paddingRight="20" 
			  paddingTop="20">
		<mx:HBox>  
			<mx:ProgressBar id="progressBar" minimum="0" maximum="100"/>
			<mx:Button label="上传导入文件" click="upload()"/>  
		</mx:HBox>  
		<mx:DataGrid id="dg"/>
		<mx:Button label="获取表格数据" click="gainDgData(event)"/>
		<mx:TextArea id="gainData" editable="false"/>
	</mx:Panel>
	
</s:WindowedApplication>



感谢网友贡献的as3xlsUTF8.swc : http://pan.baidu.com/share/link?shareid=1679735864&uk=3173173520?

api:http://pan.baidu.com/share/link?shareid=1683301117&uk=3173173520

代码下载:http://pan.baidu.com/share/link?shareid=1661052420&uk=3173173520

excel导入导出:http://pan.baidu.com/share/link?shareid=1670602033&uk=3173173520

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读