在FLEX中使用MecGrid的excel导出功能

环境:FLEX4.6.0
附件为Mecgrid导出EXCEL帮助类库

<?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:MecGrid="com.mechan.MecGrid.*"
			   xmlns:mx="library://ns.adobe.com/flex/mx" 
			   minWidth="955" minHeight="600"
			   creationComplete="MakeData()"
			   >
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
	</fx:Declarations>
	<fx:Script>
		<![CDATA[
			import mx.controls.Alert;
			
			import com.mechan.MecGrid.mecGridClasses.CellOp;
			import com.mechan.MecGrid.mecGridClasses.MecGridConst;
			import com.mechan.MecGrid.mecGridClasses.MecGridEvent;
			
			import mx.collections.ArrayCollection;
			import mx.utils.Base64Encoder;
			
			private var dpcol:ArrayCollection;
			private var dp:Array;
			
			import com.mechan.export.MecExporter;
			private function exportToExcelFile():void 
			{ 
				
				var mgrid:MecGrid=this.mgrid; 
				
				if (mgrid) 
				{ 
					try 
					{ 
						var exp:MecExporter=new MecExporter(); 
						
						// add MecGrid with sheetname 
						exp.charset="zh_CN"; 
						exp.AddDataGrid(mgrid, ""); 
						// exporting to binary data 
						var ebt:ByteArray=exp.Export2BiffExcel(); 
						
						var b64encoder:Base64Encoder=new Base64Encoder(); 
						b64encoder.encodeBytes(ebt); 
						var file:FileReference=new FileReference(); 
						file.save(ebt, "t.xls"); 
						/* b64encoder.insertNewLines = false; 
						b64encoder.encodeBytes(ebt); 
						ExternalInterface.call("loadExcelFile", b64encoder.flush()); */ 
					} 
					catch (e:Error) 
					{ 
						mx.controls.Alert.show(e.message);
					} 
				} 
			} 
			
			private function MakeData():void{
				var i:int;
				var n:int = 0;
				
				dp = new Array();
				
				var obj:Object = new Object();
				//obj.continent = "continent1";
				obj.country = "我是来自";
				obj.city = "city1";
				obj.qt1 = Math.random() * 100;
				obj.qt2 = Math.random() * 100;
				obj.qt3 = Math.random() * 100;
				obj.qt4 = Math.random() * 100;
				dp.push(obj);
				
				var obj2:Object = new Object();
				//obj2.continent = "continent1";
				obj2.country = "我是来自";
				obj2.city = "city1" ;
				obj2.qt1 = Math.random() * 100;
				obj2.qt2 = Math.random() * 100;
				obj2.qt3 = obj.qt3;
				obj2.qt4 = obj.qt3;
				dp.push(obj2);
				
				
				mgrid.redraw = false;
				mgrid.SelectionMode = CellOp.SELECTIONMODE_CELL | CellOp.SELECT_MULTI;
				mgrid.Rows.count = 0;
				mgrid.Cols.count = 7;
				
				mgrid.spreadSheetStyle = true;
				
				mgrid.merge_option = MecGridConst.MERGE_PREV;
				
				mgrid.AddRow(0, "CONTINENT;COUNTRY;CITY;QT1;QT2;QT3;QT4");
				mgrid.Rows.fixed = 1;
				
				mgrid.Cols.item(0).dataField = "continent";
				mgrid.Cols.item(1).dataField = "country";
				mgrid.Cols.item(2).dataField = "city";
				mgrid.Cols.item(3).dataField = "qt1";
				mgrid.Cols.item(4).dataField = "qt2";
				mgrid.Cols.item(5).dataField = "qt3";
				mgrid.Cols.item(6).dataField = "qt4";
				
				mgrid.Cols.item(0).merge = true;
				mgrid.Cols.item(1).merge = true;
				mgrid.Cols.item(2).merge = true;
				mgrid.Cols.item(3).merge = true;
				mgrid.Cols.item(4).merge = true;
				mgrid.Cols.item(5).merge = true;
				mgrid.Cols.item(6).merge = true;
				
				mgrid.Cols.item(3).datatype = CellOp.STATUSBAR;
				mgrid.Cols.item(4).datatype = CellOp.STATUSBAR;
				
				//				mgrid.addEventListener(MecGridEvent.DATAPROVIDER_UPDATE_FINISHED, refreshRandomValues);
				mgrid.treeGridData = false;
				mgrid.dataProvider = dp;
				mgrid.redraw = true;
			}
			
		]]>
	</fx:Script>
	<s:layout>
		<s:VerticalLayout gap="2" />
	</s:layout>
	<MecGrid:MecGrid id="mgrid" width="100%" height="100%">
		<!--MecGrid:ResourceXML >
		<fx:XML xmlns="">
		<List>
		<option cols="7" fixedrows="2" fixedcols="0" merge_option="4" merge_option_fixedcolumn="0" merge_option_fixedrow="2" showspreadsheet="F" selectionmode="18" treeview="T" treeColumn="0" textalignment="6" textalignment_fixed="5"/>
		<columns>
		<column name="col_0" datafield="selected" visible="T" editable="F" colindex="0" autowidth="T" width="41" datatype="0" textalign="5" textalign_fixed="10" formatstring="" header="Select;Select"/>
		<column name="col_1" datafield="country" visible="T" editable="F" colindex="1" autowidth="T" width="61" datatype="5" textalign="4" textalign_fixed="10" formatstring="" header="Country;Country"/>
		<column name="col_2" datafield="city" visible="T" editable="F" colindex="2" autowidth="T" width="61" datatype="5" textalign="4" textalign_fixed="10" formatstring="" header="City;City"/>
		<column name="col_3" datafield="qt1" visible="T" editable="F" colindex="3" autowidth="T" width="88" datatype="5" textalign="10" textalign_fixed="10" formatstring="#,###.##" header="Visitors;1st Quater (A)"/>
		<column name="col_4" datafield="qt2" visible="T" editable="F" colindex="4" autowidth="T" width="92" datatype="5" textalign="10" textalign_fixed="10" formatstring="#,###.##" header="Visitors;2nd Quater (B)"/>
		<column name="col_5" datafield="qt3" visible="T" editable="F" colindex="5" autowidth="T" width="90" datatype="5" textalign="10" textalign_fixed="10" formatstring="#,###.##" header="Visitors;3rd Quater (C)"/>
		<column name="col_6" datafield="qt4" visible="T" editable="F" colindex="6" autowidth="T" width="90" datatype="5" textalign="10" textalign_fixed="10" formatstring="#,###.##" header="Visitors;4th Quater (D)"/>
		</columns>
		</List>    
		</fx:XML>
		</MecGrid:ResourceXML-->
	</MecGrid:MecGrid>
	<s:Button click="exportToExcelFile()" label="导出EXCEL"/>
	
</s:Application>


你可能感兴趣的:(Flex,excel导出,MecGrid)