FLEX无法直接导出EXCEL等格式文件,解决方法是将数据传到后台后导出,此方法导出的EXCEL文件实际为文本方式,无法实现需要。
FLEX:
apply.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" fontSize="12">
<mx:Script source="myfunction/utils.as"/>
<mx:RemoteObject id="studentService" destination="studentService">
<mx:method name="createExcel" result="resultCreateExcel(event)" fault="faultCreateExcel(event)" />
</mx:RemoteObject>
<mx:ArrayCollection id="dgArray">
<mx:Object pid="1" name="秦始皇" time="秦" />
<mx:Object pid="2" name="汉高祖" time="汉" />
<mx:Object pid="3" name="唐太宗" time="唐" />
</mx:ArrayCollection>
<mx:DataGrid right="633" left="10" bottom="10" top="10" id="dgList" mouseDown="" dataProvider="{dgArray}"
horizontalGridLineColor="#092537">
<mx:columns>
<mx:DataGridColumn headerText="序号" dataField="id"/>
<mx:DataGridColumn headerText="名字" dataField="name"/>
<mx:DataGridColumn headerText="朝代" dataField="time"/>
</mx:columns>
</mx:DataGrid>
<mx:Button x="691" y="56" label="导出EXCEL" click="loadDGInExcel(dgList,'http://localhost:8080/myApply/testExcelExport.jsp');"/>
</mx:Application>
utils.as
import flash.errors.*;
import flash.events.*;
import flash.external.*;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.net.navigateToURL;
import mx.collections.ArrayCollection;
import mx.controls.DataGrid;
private function convertDGToHTMLTable(dg:DataGrid,dataArray:ArrayCollection):String {
var font:String = dg.getStyle('fontFamily');
var size:String = dg.getStyle('fontSize');
var str:String = '';
var colors:String = '';
var style:String = 'style="font-family:'+font+';font-size:'+size+'pt;"';
var hcolor:Array;
if(dg.getStyle("headerColor") != undefined) {
hcolor = [dg.getStyle("headerColor")];
} else {
hcolor = dg.getStyle("headerColors");
}
str+= '<table width="'+dg.width+'" border="1"><thead><tr width="'+dg.width+'" style="background-color:#' +Number((hcolor[0])).toString(16)+'">';
for(var i:int = 0;i<dg.columns.length;i++) {
colors = dg.getStyle("themeColor");
if(dg.columns[i].headerText != undefined) {
str+="<th "+style+">"+dg.columns[i].headerText+"</th>";
} else {
str+= "<th "+style+">"+dg.columns[i].dataField+"</th>";
}
}
str += "</tr></thead><tbody>";
colors = dg.getStyle("alternatingRowColors");
for(var j:int =0;j<dataArray.length;j++) {
str+="<tr width=/""+Math.ceil(dg.width)+"/">";
for(var k:int=0; k < dg.columns.length; k++) {
if(dataArray.getItemAt(j) != undefined && dataArray.getItemAt(j) != null) {
if(dg.columns[k].labelFunction != undefined) {
str += "<td width=/""+Math.ceil(dg.columns[k].width)+"/" "+style+">"+dg.columns[k].labelFunction(dataArray.getItemAt(j),dg.columns[k].dataField)+"</td>";
} else {
str += "<td width=/""+Math.ceil(dg.columns[k].width)+"/" "+style+">"+dataArray.getItemAt(j)[dg.columns[k].dataField]+"</td>";
}
}
}
str += "</tr>";
}
str+="</tbody></table>";
return str;
}
public function loadDGInExcel(dg:DataGrid,url:String):void {
var variables:URLVariables = new URLVariables();
variables.htmltable = convertDGToHTMLTable(dg,dg.dataProvider as ArrayCollection);
var u:URLRequest = new URLRequest(url);
u.data = variables;
u.method = URLRequestMethod.POST;
navigateToURL(u,"_self");
}
JAVA:
ExcelExport.jsp
<%@ page language="java"%>
<%@ page contentType="application/msexcel;charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
//response.setHeader("Content-disposition","attachment; filename=test");
response.setHeader("Content-disposition","attachment; filename=test.xls");
String str = request.getParameter("htmltable");
out.print(str);
%>