Export Data in Excel/CSV fromat in Flex

Hi All,

In flex exporting a data is just very easy, All you have to do is to write the below method and customize accordingly.
DataGridDataExporter is the flex object which does the job in magical way.

Step 1: In MXML file
public static function exportAction(gridObj:DataGrid, fileName:String):void{
var exportDataString:String = “”;
if(ArrayCollection(gridObj.dataProvider).length > 0){
var csvConverter :DataGridDataExporter = new DataGridDataExporter();
exportDataString=DataGridDataExporter.toCSV(gridObj);
var url : String = “/{app_context}/exportDataToExcel”; // add you servlet/jsp path
var urlr : URLRequest = new URLRequest(url);
urlr.method = “POST”; // “Can set GET
var urlv : URLVariables = new URLVariables();
urlv.gridData1 = exportDataString;
if(fileName==null || fileName ==””){
fileName=”Data File”; // default Name
}
urlv.fileName = fileName;
urlr.data = urlv;
navigateToURL(urlr,”_blank”);
}
}

Step 2: Write down a servlet

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String csvData = “”;
String type;

type = request.getParameter(“type”);
csvData = request.getParameter(“gridData1”);
response.setContentType(“application/x-download”);
response.setHeader(“Content-Disposition”, “attachment; filename=”+request.getParameter(“fileName”)+”.csv”);
response.getOutputStream().print(csvData);
}

Thanks
R Vashi