微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

flex4 与报表

在传统的web语言例如jsp或者asp对于将结果导出成excel报表形式已经变得相当简单和成熟了,在flex里面是如何将表格单元格的数据导出来的呢?

adobe公司给我们提供了一种解决方案:首先将表格里面的数据序列化成为字符串,然后利用urlvariables和URLLoader类将字符串发送到服务器端脚本,在服务器端加上文件头,指定filename和contentType,利用浏览器在客户端打印出来。贴出转换的函数代码

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("alternatingRowColors");
 
 //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++) {    
   if(dg.dataProvider.getItemAt(j) != undefined && dg.dataProvider.getItemAt(j) != null) {
    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 {
     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;
}

private function loadDGInExcel(dg : DataGrid,url : String):void {
 if(isExport) {
  return ;
 }
 
 isExport = true;//next export not permit

 var variables:urlvariables = new urlvariables(); 
 variables.htmltable = convertDGToHTMLTable(dg);
 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
 
    navigatetoURL(u,"_top");
    isExport = false;//next export permit
}    

下面贴出服务器的jsp页面代码
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page contentType="application/vnd.ms-word;charset=GB2312"%>
<%
 request.setCharacterEncoding("UTF-8");
 response.setHeader("Content-disposition","attachment; filename=data.doc");
 String str = request.getParameter("htmltable");
 //str = new String(str.getBytes("UTF-8"),"GB2312");
 out.print(str);
 //<%@ page contentType="application/vnd.ms-word; charset=gb2312" % response.setHeader("Content-disposition","attachment; filename=data.xls");
 //Excel为%@ page contentType="application/msexcel; charset=gb2312" %
%>

函数代码太多,简单讲一讲,第一个函数一个datagrid序列化,第二个函数发送请求,jsp接受请求并且加上头部文件

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐