0

我在这里找到了以下代码,只是我在我的 Adob​​e 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);
        }
    }
4

1 回答 1

0

After you get the byte Array you have to use a FileReference.save(bytes); call to pop up the window that prompts the user for where to save the data... as Flextras/Jeffry says the data grid is probably changing due to the dataGridResult.dataProvider line. If you're doing this as an AIR based desktop app you can use the File and FileStream class to write out the bytes directly without prompting the user.

So long as you have FP 10+ targetted

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

For 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()

于 2012-07-16T01:42:55.637 回答