`
sungang_1120
  • 浏览: 308633 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类

Flex利用as3xls-swc导出Execl文件

阅读更多

首先要先下载as3xls-swc文件 导入到项目中

 

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
	<fx:Script>
		<![CDATA[
			import com.as3xls.xls.ExcelFile;
			import com.as3xls.xls.Sheet;
			
			import mx.controls.Alert;
			
			private var sheet : Sheet;

			protected function button1_clickHandler(event:MouseEvent):void
			{
				var str : String;
				str = te.text;
				str = str.replace(/^\*|\s*$/g,"").split(" ").join("");
				
				if(!sheet){
					sheet = new Sheet();
					sheet.resize(5,3);
				}
				
				sheet.setCell(0,1,str);
				
				sheet.setCell(1,0,"姓名");
				sheet.setCell(1,1,"年龄");
				sheet.setCell(1,2,"学历");
				
				sheet.setCell(2,0,"张三");
				sheet.setCell(2,1,"21");
				sheet.setCell(2,2,"专科");
				
				sheet.setCell(3,0,"李四");
				sheet.setCell(3,1,"24");
				sheet.setCell(3,2,"本科");
					
				sheet.setCell(4,0,"王五");
				sheet.setCell(4,1,"22");
				sheet.setCell(4,2,"研究生");
				
				var xls : ExcelFile = new ExcelFile();
				xls.sheets.addItem(sheet);
				
				var bytes : ByteArray = xls.saveToByteArray();
				var fr : FileReference = new FileReference();
				
				try
				{
					fr.save(bytes,"测试表.xlsx");
				}catch(error:Error)
				{
					trace("Failed:", error.message)
					//Alert.show((error.message,"错误");
				}
				
			}

		]]>
	</fx:Script>
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
	</fx:Declarations>
	<s:Panel x="85" y="46" width="250" height="200" title="Excel导出">
		<s:Button x="108" y="92" label="导出" click="button1_clickHandler(event)"/>
		<s:Label x="28" y="45" text="标题" fontSize="15"/>
		<s:TextInput x="79" y="41" id="te"/>
	</s:Panel>
</s:Application>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics