flex导出excel(jsp)
最近新学的后台为jsp的导出excel文件的实例,感觉比jfreechart插件方便很多,现将源码贴上
mxml文件:
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml“
layout=”vertical”
initialize=”myService.send()”
fontSize=”12″ viewSourceURL=”srcview/index.html”>
<mx:Script source=”utils.as”/>
<!–<mx:Style source=”DGtoExcel.css”/>–>
<mx:Script>
<![CDATA[
import mx.events.ItemClickEvent;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.collections.ArrayCollection;
[Bindable]
private var myAC:ArrayCollection;
private function faultHandler(event:FaultEvent):void
{
Alert.show(event.fault.faultString, event.fault.message);
}
]]>
</mx:Script>
<mx:HTTPService id=”myService”
showBusyCursor=”true”
url=”data/student.xml”
result=”myAC = event.result.item.student”
fault=”faultHandler(event)”/>
<mx:DataGrid id=”myDG” width=”100%” rowCount=”20″
dataProvider=”{myAC}” >
<mx:columns>
<mx:DataGridColumn headerText=”学好” dataField=”studentID”/>
<mx:DataGridColumn headerText=”姓名” dataField=”name”/>
<mx:DataGridColumn headerText=”性别” dataField=”gender” width=”50″/>
<mx:DataGridColumn headerText=”生日” dataField=”birthday” />
<mx:DataGridColumn headerText=”班级” dataField=”className”/>
</mx:columns>
</mx:DataGrid>
<mx:Button label=”导出为Excel” click=”loadDGInExcel(myDG,’http://localhost:8080/test/index.jsp’);” />
</mx:Application>
as文件:
private function convertDGToHTMLTable(dg:DataGrid):String {
//Set default values
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;
//Retrieve the headercolor
if(dg.getStyle(“headerColor”) != undefined) {
hcolor = [dg.getStyle("headerColor")];
} else {
hcolor = dg.getStyle(“headerColors”);
}
//Set the htmltabel based upon knowlegde from the datagrid
str+= ‘<table width=”‘+dg.width+’” border=”1″><thead><tr width=”‘+dg.width+’” style=”background-color:#’ +Number((hcolor[0])).toString(16)+’”>’;
//Set the tableheader data (retrieves information from the datagrid header
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(“alternatingItemColors”);
//Loop through the records in the dataprovider and
//insert the column information into the table
for(var j:int =0;j<dg.dataProvider.length;j++) {
str+=”<tr width=\”"+Math.ceil(dg.width)+”\”>”;
for(var k:int=0; k < dg.columns.length; k++) {
//Do we still have a valid item?
if(dg.dataProvider.getItemAt(j) != undefined && dg.dataProvider.getItemAt(j) != null) {
//Check to see if the user specified a labelfunction which we must
//use instead of the dataField
if(dg.columns[k].labelFunction != undefined) {
str += “<td width=\”"+Math.ceil(dg.columns[k].width)+”\” “+style+”>”+dg.columns[k].labelFunction(dg.dataProvider.getItemAt(j),dg.columns[k].dataField)+”</td>”;
} else {
//Our dataprovider contains the real data
//We need the column information (dataField)
//to specify which key to use.
str += “<td width=\”"+Math.ceil(dg.columns[k].width)+”\” “+style+”>”+dg.dataProvider.getItemAt(j)[dg.columns[k].dataField]+”</td>”;
}
}
}
str += “</tr>”;
}
str+=”</tbody></table>”;
return str;
}
/**
* Load a specific datagrid into Excel
* This method passes the htmltable string to an backend script which then
* offers the excel download to the user.
* The reason for not using a copy to clipboard and then javascript to
* insert it into Excel is that this mostly will fail because of the user
* setup (Webbrowser configuration).
*
* @params: dg Datagrid The Datagrid that will be loaded into Excel
* @params: url String The location of the excel export file
*/
private function loadDGInExcel(dg:DataGrid,url:String):void {
//Pass the htmltable in a variable so that it can be delivered
//to the backend script
var variables:URLVariables = new URLVariables();
variables.htmltable = convertDGToHTMLTable(dg);
//Setup a new request and make sure that we are
//sending the data through a post
var u:URLRequest = new URLRequest(url);
u.data = variables; //Pass the variables
u.method = URLRequestMethod.POST; //Don’t forget that we need to send as POST
//Navigate to the script
//We can use _self here, since the script will through a filedownload header
//which results in offering a download to the user (and still remaining in you Flex app.)
navigateToURL(u,”_self”);
}
xml文件:
<?xml version=”1.0″ encoding=”UTF-8″?>
<item>
<student studentID=”S1-200501″ name=”张琦” gender=”0″ birthday=”1986-03-12″ className=”软件一班” />
<student studentID=”S1-200502″ name=”张杰” gender=”1″ birthday=”1986-03-12″ className=”软件一班” />
<student studentID=”S1-200503″ name=”陈磊” gender=”1″ birthday=”1985-03-06″ className=”软件一班” />
<student studentID=”S1-200504″ name=”杨龙武” gender=”1″ birthday=”1985-07-15″ className=”软件一班” />
<student studentID=”S1-200505″ name=”王雪纯” gender=”0″ birthday=”1986-09-09″ className=”软件一班” />
<student studentID=”S1-200507″ name=”杨耀宇” gender=”1″ birthday=”1986-05-09″ className=”软件一班” />
<student studentID=”S1-200509″ name=”王孝益” gender=”1″ birthday=”1985-02-07″ className=”软件一班” />
<student studentID=”S1-200510″ name=”王瑛” gender=”0″ birthday=”1985-04-08″ className=”软件一班” />
<student studentID=”S1-200511″ name=”张强” gender=”1″ birthday=”1985-02-06″ className=”软件一班” />
<student studentID=”S1-200512″ name=”刘伟” gender=”1″ birthday=”1986-12-06″ className=”软件一班” />
<student studentID=”S1-200513″ name=”周婷” gender=”0″ birthday=”1984-04-09″ className=”软件一班” />
<student studentID=”S1-200514″ name=”刘明月” gender=”0″ birthday=”1986-05-04″ className=”软件一班” />
<student studentID=”S1-200515″ name=”张颖捷” gender=”0″ birthday=”1985-06-05″ className=”软件一班” />
<student studentID=”S1-200516″ name=”王晓双” gender=”0″ birthday=”1986-07-05″ className=”软件一班” />
<student studentID=”S1-200517″ name=”李婷” gender=”0″ birthday=”1985-08-06″ className=”软件一班” />
<student studentID=”S2-200501″ name=”张俊” gender=”1″ birthday=”1985-09-16″ className=”软件二班” />
<student studentID=”S2-200502″ name=”李柯” gender=”1″ birthday=”1985-06-18″ className=”软件二班” />
<student studentID=”S2-200503″ name=”张弛” gender=”1″ birthday=”1985-12-24″ className=”软件二班” />
<student studentID=”S2-200504″ name=”李杰” gender=”1″ birthday=”1984-10-20″ className=”软件二班” />
<student studentID=”S2-200505″ name=”李小维” gender=”0″ birthday=”1986-08-08″ className=”软件二班” />
<student studentID=”S2-200506″ name=”李玲” gender=”0″ birthday=”1986-06-28″ className=”软件二班” />
<student studentID=”S2-200507″ name=”刘军杰” gender=”1″ birthday=”1984-12-10″ className=”软件二班” />
<student studentID=”S2-200508″ name=”李志军” gender=”1″ birthday=”1985-03-08″ className=”软件二班” />
<student studentID=”S2-200509″ name=”李林” gender=”1″ birthday=”1985-07-20″ className=”软件二班” />
<student studentID=”S2-200510″ name=”赵媛” gender=”0″ birthday=”1986-07-12″ className=”软件二班” />
<student studentID=”S2-200511″ name=”赵莎莎” gender=”0″ birthday=”1986-08-16″ className=”软件二班” />
<student studentID=”S2-200512″ name=”赵楠楠” gender=”0″ birthday=”1986-05-08″ className=”软件二班” />
<student studentID=”S2-200513″ name=”赵嘉琦” gender=”0″ birthday=”1986-05-08″ className=”软件二班” />
<student studentID=”S2-200514″ name=”孙博” gender=”1″ birthday=”1985-07-02″ className=”软件二班” />
<student studentID=”S2-200515″ name=”孙旭超” gender=”1″ birthday=”1985-04-02″ className=”软件二班” />
<student studentID=”W1-200501″ name=”谭壮” gender=”1″ birthday=”1985-12-30″ className=”网络一班” />
<student studentID=”W1-200502″ name=”周婷” gender=”0″ birthday=”1986-06-14″ className=”网络一班” />
<student studentID=”W1-200503″ name=”林丽” gender=”0″ birthday=”1985-08-12″ className=”网络一班” />
<student studentID=”W1-200504″ name=”卢迪” gender=”0″ birthday=”1986-12-02″ className=”网络一班” />
<student studentID=”W1-200505″ name=”梁家荣” gender=”1″ birthday=”1985-07-02″ className=”网络一班” />
<student studentID=”W1-200506″ name=”陈兴盛” gender=”1″ birthday=”1984-03-19″ className=”网络一班” />
<student studentID=”W1-200507″ name=”张强” gender=”1″ birthday=”1984-04-18″ className=”网络一班” />
<student studentID=”W1-200508″ name=”刘军” gender=”1″ birthday=”1986-12-11″ className=”网络一班” />
<student studentID=”W1-200509″ name=”周坚” gender=”1″ birthday=”1984-12-17″ className=”网络一班” />
<student studentID=”W1-200510″ name=”张顺” gender=”1″ birthday=”1984-05-18″ className=”网络一班” />
<student studentID=”W1-200511″ name=”刘若水” gender=”0″ birthday=”1985-07-09″ className=”网络一班” />
<student studentID=”W1-200512″ name=”张力” gender=”1″ birthday=”1984-09-16″ className=”网络一班” />
<student studentID=”W1-200513″ name=”陈睿” gender=”0″ birthday=”1985-10-12″ className=”网络一班” />
<student studentID=”W1-200514″ name=”孔蔼” gender=”0″ birthday=”1985-12-15″ className=”网络一班” />
<student studentID=”W1-200515″ name=”韩轩” gender=”1″ birthday=”1985-11-03″ className=”网络一班” />
<student studentID=”W1-200516″ name=”李清” gender=”0″ birthday=”1986-05-25″ className=”网络一班” />
<student studentID=”W1-200517″ name=”王良” gender=”1″ birthday=”1985-04-24″ className=”网络一班” />
<student studentID=”W1-200518″ name=”陈诚” gender=”1″ birthday=”1985-07-15″ className=”网络一班” />
<student studentID=”W1-200519″ name=”王洋” gender=”0″ birthday=”1984-06-10″ className=”网络一班” />
<student studentID=”W1-200521″ name=”张扬” gender=”1″ birthday=”1985-06-02″ className=”网络一班” />
<student studentID=”W2-200501″ name=”刘容” gender=”0″ birthday=”1985-12-31″ className=”网络二班” />
<student studentID=”W2-200503″ name=”李杰” gender=”1″ birthday=”1985-07-08″ className=”网络二班” />
<student studentID=”W2-200504″ name=”张义” gender=”1″ birthday=”1985-08-14″ className=”网络二班” />
<student studentID=”W2-200505″ name=”李慧” gender=”0″ birthday=”1985-04-08″ className=”网络二班” />
<student studentID=”W2-200506″ name=”张俊生” gender=”1″ birthday=”1986-06-19″ className=”网络二班” />
<student studentID=”W2-200507″ name=”李从文” gender=”1″ birthday=”1985-08-16″ className=”网络二班” />
<student studentID=”W2-200508″ name=”张恒” gender=”1″ birthday=”1985-06-20″ className=”网络二班” />
<student studentID=”W2-200509″ name=”陈明” gender=”0″ birthday=”1984-04-11″ className=”网络二班” />
<student studentID=”W2-200510″ name=”周勤” gender=”0″ birthday=”1985-08-20″ className=”网络二班” />
<student studentID=”W2-200511″ name=”郭英” gender=”0″ birthday=”1985-02-02″ className=”网络二班” />
<student studentID=”W2-200512″ name=”李明” gender=”1″ birthday=”1984-04-09″ className=”网络二班” />
<student studentID=”W2-200513″ name=”叶文娟” gender=”0″ birthday=”1985-08-03″ className=”网络二班” />
<student studentID=”W2-200514″ name=”李思思” gender=”0″ birthday=”1986-12-15″ className=”网络二班” />
<student studentID=”W2-200515″ name=”周雄” gender=”1″ birthday=”1985-07-02″ className=”网络二班” />
<student studentID=”W2-200516″ name=”梁帅” gender=”1″ birthday=”1986-12-09″ className=”网络二班” />
<student studentID=”W2-200518″ name=”梁琪” gender=”0″ birthday=”1986-12-14″ className=”网络二班” />
<student studentID=”W2-200519″ name=”王维” gender=”1″ birthday=”1985-05-20″ className=”网络二班” />
<student studentID=”W2-200520″ name=”陈功” gender=”1″ birthday=”1984-11-01″ className=”网络二班” />
<student studentID=”W2-200521″ name=”李静” gender=”0″ birthday=”1986-12-14″ className=”网络二班” />
<student studentID=”W2-200522″ name=”李盈盈” gender=”0″ birthday=”1984-02-12″ className=”网络二班” />
</item>
以上是源码,本人只是引用学习,著作权归作者所有,后期本人会在as部分加上注释,纯属个人理解,有不对的地方还请高手纠正
jsp文件:
<%@ page language=”java” import=”java.util.*” pageEncoding=”utf-8″%>
<%@page contentType=”application/msexcel;charset=utf-8″ %>
<%
request.setCharacterEncoding(“utf-8″);
response.setHeader(“Content-disposition”,”attachment;filename=test.xls”);//contentType=”application/vnd.ms-excel” 在浏览器中打开
String str=request.getParameter(“htmltable”);//htmltable是flex里传过来的参数
System.out.println(str);
out.print(str);
//flex的编码格式为utf-8
//用于flex的导出报表
%>


