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

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.setHeader(“Content-Disposition”, “attachment; filename=”+request.getParameter(“fileName”)+”.csv”);

R Vashi

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.