Flex导出DataGrid数据

Flex前端+Java后台,要将Flex界面中AdvancedDataGrid的数据导出成Excel文件。有2种方法:

1.直接在前台导出。
    使用开源项目as3xls,下载个解决了中文乱码的as3xlsUTF8.swc包,加到Flex工程的引用库中。
    AS代码,点击“导出Excel”按钮后的函数:
Java代码 复制代码 收藏代码
var sheet:Sheet = new Sheet();  
private function export2ExcelHandel(e:MouseEvent):void 
{  
    //在前台生成Excel  
    var excelFile:ExcelFile=new ExcelFile();  
    var arr:ArrayCollection=myTable.dataProvider as ArrayCollection;  
    for(var i:int=0; i<arr.length; i++)  
    {  
        var obj:Object=arr[i];  
        var j:int=0;  
        for(var key:String in obj)  
        {  
            excelFile.sheets.addItem(generateSheet(0,j,key));     
            excelFile.sheets.addItem(generateSheet(i,j,obj[key]));  
            j++;  
        }  
    }         
    var mbytes:ByteArray=excelFile.saveToByteArray();  
    var file:FileReference=new FileReference();  
    file.save(mbytes,"测试文件.xls");   
}  
              
function generateSheet(i:int,j:int,o:Object):Sheet{  
    if(!sheet){      
        sheet = new Sheet();      
        sheet.resize(10,10);      
    }      
    sheet.setCell(i, j, String(o));      
    return sheet;      
}     
Java代码  收藏代码
var sheet:Sheet = new Sheet(); 
private function export2ExcelHandel(e:MouseEvent):void 

    //在前台生成Excel 
    var excelFile:ExcelFile=new ExcelFile(); 
    var arr:ArrayCollection=myTable.dataProvider as ArrayCollection; 
    for(var i:int=0; i<arr.length; i++) 
    { 
        var obj:Object=arr[i]; 
        var j:int=0; 
        for(var key:String in obj) 
        { 
            excelFile.sheets.addItem(generateSheet(0,j,key));    
            excelFile.sheets.addItem(generateSheet(i,j,obj[key])); 
            j++; 
        } 
    }        
    var mbytes:ByteArray=excelFile.saveToByteArray(); 
    var file:FileReference=new FileReference(); 
    file.save(mbytes,"测试文件.xls");  

             
function generateSheet(i:int,j:int,o:Object):Sheet{ 
    if(!sheet){     
        sheet = new Sheet();     
        sheet.resize(10,10);     
    }     
    sheet.setCell(i, j, String(o));     
    return sheet;     
}     

FileReference的save方法在Flash10才支持,
在Flex工程上右键,属性,Flex Conpiler中把Require Flash Player version:填为10.0.0;在Flex Build Path中加入playerglobal.swc
,路径为…\sdks\3.2.0\frameworks\libs\player\10\playerglobal.swc

2.通过调JSP或Servlet导出。
AS代码:
Java代码 复制代码 收藏代码
public static function ToExcel(dg:AdvancedDataGrid,name:String)  
{  
    //调用JSP  
    var url:String="http://localhost:8080/XXX/Export/ExcelExport.jsp";  
    var variables:URLVariables = new URLVariables();   
    variables.htmltable = convertDGToHTMLTable(dg);  
    var u:URLRequest = new URLRequest(url);  
    u.data = variables;  
    u.method = URLRequestMethod.POST;  
    navigateToURL(u,"_self");   
}  
 
private static function convertDGToHTMLTable(dg:AdvancedDataGrid):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");  
    }                  
         
    str+= '<head><meta http-equiv="Content-Type" content="text/htm; charset=utf-8"></head><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<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;  

Java代码  收藏代码
public static function ToExcel(dg:AdvancedDataGrid,name:String) 

    //调用JSP 
    var url:String="http://localhost:8080/XXX/Export/ExcelExport.jsp"; 
    var variables:URLVariables = new URLVariables();  
    variables.htmltable = convertDGToHTMLTable(dg); 
    var u:URLRequest = new URLRequest(url); 
    u.data = variables; 
    u.method = URLRequestMethod.POST; 
    navigateToURL(u,"_self");  

 
private static function convertDGToHTMLTable(dg:AdvancedDataGrid):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"); 
    }                 
        
    str+= '<head><meta http-equiv="Content-Type" content="text/htm; charset=utf-8"></head><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<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; 



ExcelExport.jsp放在后台XXX工程的WebRoot下的Export文件夹中。
ExcelExport.jsp:
Java代码 复制代码 收藏代码
<%@ page language="java"%>  
<%@ page contentType="application/msexcel;charset=UTF-8" 
    pageEncoding="UTF-8"%>  
<%  
request.setCharacterEncoding("UTF-8");  
//String instr=request.getParameter("filename");  
response.setHeader("Content-disposition","attachment; filename=excel.xls");  
response.setCharacterEncoding("UTF-8");  
String str = request.getParameter("htmltable");  
out.print(str);  
%> 

你可能感兴趣的:(Flex,Flash,Adobe)