How to export a datagrid to Excel file in Flex?
How do I export data in my datagrid to an Excel file in Flex?
Can 开发者_运维技巧anyone provide some examples for that? I am browsing but couldn't find out a single example of that kind.
EDIT
Browsed a lot and found out how to convert datagrid data to csv format. Now How to convert that to excel? Is there a way to do that without using any server script ? Can't it be done in Flex alone?
There is as3xls for you to write xls file. It only support a single sheet(but I think that's fine).
But I think using csv or html as stated by @susichan and @Rafal Ziolkowski will be more simple if you do not need to use excel functions (like cell formula).
Oh, and there is csvlib for writing csv. For html, do it like writing XML will be fine.
If you have a Java servlet back-end you can use a servlet to output the file.
My only Flex-only idea was to output the CSV data to a popup with a textarea that could be copied and pasted into a file by the user.
Excel reads the HTML table as a kind of spreadsheet. Just read the grid row by row, column by column and produce a set of HTML table cells and produce a file named whatever.xls.
First Download swc file from the following Link
Link
Now you need to do little bit of work just copy that code and give column names appropriately.
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);
}
}
here is your answer
http://learnflexair.wordpress.com/category/uncategorized/air/
as3xls seems to read fine but writing Excel files is another story. It can't write multisheet workbooks, it [most of the time] can't read it's own files, and when Excel can read what as3xls spits out you have to do multiple saves to get all the garbage out. A real bummer. I'm working on using AIR 2.0's NativeProcess to call a Python script to do the writing and I guess I'll have to export the datagrid/ArrayCollection to CSV first... seems like a long way around. Can't believe there's not a better ActionScript option for mucking with Excel. Is it really that rare of a task?
精彩评论