专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Flex

Flex导出Excel如何做?要详细代码和说明。多谢

发布时间:2011-06-27 20:47:21 文章来源:www.iduyao.cn 采编人员:星星草
Flex导出Excel怎么做?要详细代码和说明。谢谢
如题。

------解决方案--------------------
给你一个网上的例子,希望对你有帮助,不过里面的代码写的不太好,很乱。
JScript code


/**
 * Simple script to convert a Datagrid to a HTML table and then 
 * pass it on to an external excel exporter
 *
 */    

//Libs that are mostly used 
//(only a number are necessary for the datagrid conversion and export)
import flash.errors.*;
import flash.events.*;
import flash.external.*;
import flash.net.URLRequest;
import flash.net.URLVariables;

/**
 * Convert the datagrid to a html table
 * Styling etc. can be done externally
 * 
 * @param: dg Datagrid Contains the datagrid that needs to be converted
 * @returns: String
 */
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");
}
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: