简体   繁体   中英

export to excel datagrid adobe flex3

I found the following code here only I kept the same in my Adobe Flex project but after clicking on the export to Excel button I can't see any Excel opening in addition to that my datagrid is filling with the column names which I intended to give to my 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);
        }
    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM