导出到excel datagrid adobe flex3

时间:2012-07-15 18:28:17

标签: actionscript-3 flex flex3

我在这里发现以下代码只是在我的Adobe Flex项目中保持不变但是在点击导出到Excel按钮之后我看不到任何Excel打开,除了我的数据网格填充了我的列名称打算提供给我的Excel。

    public function roExport_export_Result(e:ResultEvent):void
    {
        if(e.result.length != 0)
        {
            btnExportToExcel.enabled = true;

            var arrExportResult:Array = e.result as Array;

            xlsFile = new ExcelFile();
            var sheet:Sheet = new Sheet();

            sheet.resize(arrExportResult.length+1,14);

            sheet.setCell(0,0,'Id');
            sheet.setCell(0,1,'Full Name');
            sheet.setCell(0,2,'Gender');
            sheet.setCell(0,3,'Birth Date');
            sheet.setCell(0,4,'College Name');
            sheet.setCell(0,5,'Qualification');
            sheet.setCell(0,6,'Email Id');
            sheet.setCell(0,7,'Mobile');
            sheet.setCell(0,8,'Position Applied For');
            sheet.setCell(0,9,'Technology Interested');
            sheet.setCell(0,10,'User Name');
            sheet.setCell(0,11,'Password');
            sheet.setCell(0,12,'Exam Date');
            sheet.setCell(0,13,'Percentage');
            sheet.setCell(0,14,'IsActive');

            for(var i:int=0;i<arrExportResult.length;i++)
            {
                sheet.setCell(i+1, 0, arrExportResult[i].Id);
                sheet.setCell(i+1, 1, arrExportResult[i].FullName);
                if(arrExportResult[i].Gender == 1)
                {
                    arrExportResult[i].Gender = "Male"
                }
                else
                {
                    arrExportResult[i].Gender = "Female";
                }
                sheet.setCell(i+1, 2, arrExportResult[i].Gender);
                var date:String = arrExportResult[i].BirthDate.date.toString();
                var month:String = (arrExportResult[i].BirthDate.month + 1).toString();
                var year:String = arrExportResult[i].BirthDate.fullYear.toString();
                var bDate:String = date + "/" + month + "/" + year;
                arrExportResult[i].BirthDate = bDate;
                sheet.setCell(i+1, 3, arrExportResult[i].BirthDate);
                sheet.setCell(i+1, 4, arrExportResult[i].CollegeId);
                sheet.setCell(i+1, 5, arrExportResult[i].QualificationId);
                sheet.setCell(i+1, 6, arrExportResult[i].EmailId);
                sheet.setCell(i+1, 7, arrExportResult[i].Mobile);
                sheet.setCell(i+1, 8, arrExportResult[i].PositionName);
                sheet.setCell(i+1, 9, arrExportResult[i].TechForTraining);
                sheet.setCell(i+1, 10, arrExportResult[i].UserName);
                sheet.setCell(i+1, 11, arrExportResult[i].Password);
                var date:String = arrExportResult[i].CreatedDate.date.toString();
                var month:String = (arrExportResult[i].CreatedDate.month + 1).toString();
                var year:String = arrExportResult[i].CreatedDate.fullYear.toString();
                var hour:String = arrExportResult[i].CreatedDate.hours.toString();
                var min:String = arrExportResult[i].CreatedDate.minutes.toString();
                var sec:String = arrExportResult[i].CreatedDate.seconds.toString();
                var cDate:String = date + "/" + month + "/" + year + " " + hour + ":" + min + ":" + sec;
                arrExportResult[i].CreatedDate = cDate;
                sheet.setCell(i+1, 12, arrExportResult[i].CreatedDate);
                sheet.setCell(i+1, 13, arrExportResult[i].Percentage);
                sheet.setCell(i+1, 14, arrExportResult[i].IsActive);
            }

            dataGridResult.dataProvider = arrExportResult;

            xlsFile.sheets.addItem(sheet);      
            bytes = xlsFile.saveToByteArray();                  
        }
        else
        {
            arrExportResult = new Array();
            dataGridResult.dataProvider = arrExportResult;
            btnExportToExcel.enabled = false;
            xlsFile = new ExcelFile();
            var sheet:Sheet = new Sheet();
            Alert.show("No Records Found",parentApplication.alertTitle);
        }
    }

1 个答案:

答案 0 :(得分:0)

获得字节数组后,必须使用FileReference.save(bytes);调用弹出窗口,提示用户保存数据的位置......正如Flextras / Jeffry所说,由于dataGridResult.dataProvider线,数据网格可能正在改变。如果您将此作为基于AIR的桌面应用程序执行此操作,则可以使用File和FileStream类直接写出字节而不提示用户。

只要你有FP 10+目标

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/FileReference.html#save()

对于AIR:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/com/adobe/livecycle/content/File.html

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/FileStream.html#writeBytes()