Export DataGrid to CSV
by krishna
I was in difficult situation working on exporting datagrid to CSV, Excel, & PDF. The below code works for CSV & MS-Excel.
Later, I will update the code for PDF format..
DataGridExporter.as
package components.grid.export {
import mx.collections.IViewCursor;
import mx.controls.DataGrid;
import mx.controls.dataGridClasses.DataGridColumn;
public class DataGridExporter {
private static var csvSeparator:String = “,”;
private static var lineSeparator:String = “n”;
public static function exportCSV(dg:DataGrid):String {
var columns:Array = dg.columns;
var columnCount:int = columns.length;
var headerGenerated:Boolean = false;
var dataProvider:Object = dg.dataProvider;
var rowCount:int = dataProvider.length;
var cursor:IViewCursor = dataProvider.createCursor ();
var j:int = 0;
var data:String = “”; var header:String = “”;
//loop through rows
while (!cursor.afterLast) {
var obj:Object = cursor.current;
//loop through all columns for the row
for(var k:int = 0; k < columnCount; k++) {
var column:DataGridColumn = columns[k];
//Exclude column data which is invisible (hidden)
if(!column.visible) {
continue;
} // if(!column.visible)
var colData:String = column.itemToLabel(obj);
if (colData.indexOf(“,”) > 0 )
colData = (“””+ colData +”””);
data += colData;
if(k < (columnCount -1)) {
data += csvSeparator;
} // if(k < (columnCount -1))
//generate header of CSV, only if it’s not genereted yet
if (!headerGenerated) {
header += column.headerText;
if (k < columnCount – 1) {
header += csvSeparator;
} // if (k < columnCount – 1)
} // if (!headerGenerated)
} // for(var k:int = 0; k < columnCount; k++)
headerGenerated = true;
if (j < (rowCount – 1)) {
data += lineSeparator;
} // if (j < (rowCount – 1))
j++;
cursor.moveNext ();
}
return (header + lineSeparator + data);
}
}
MXML
private function exportCSV():void {
var csvData:String = DataGridExporter.exportCSV(datagridLoginHistory);
var variables:URLVariables = new URLVariables();
variables.fileData = csvData;
variables.fileName = “LoginHistory.csv”;
var u:URLRequest = new URLRequest(“ExportData.jsp”);
u.data = variables;
u.method = URLRequestMethod.POST;
navigateToURL(u,”_self”);
}
ExportData.JSP
<%@ page language=”java” contentType=”text/html;charset=UTF-8″ pageEncoding=”UTF-8″ %>
<%@page import=”java.io.*”%>
<%
String fileName = request.getParameter(“fileName”);
String fileData = request.getParameter(“fileData”);
response.setContentType(“application/octet-stream”);
response.setHeader(“Content-Disposition”, “attachment;filename=”” + fileName + “””);
try {
int byteLength = fileData.getBytes().length;
ServletOutputStream sos = response.getOutputStream();
InputStream in = new ByteArrayInputStream(fileData.getBytes(“UTF-8”));
byte[] outputByte = new byte[byteLength]; // [4096];
//copy binary contect to output stream
while(in.read(outputByte, 0, byteLength) != -1) {
sos.write(outputByte, 0, byteLength);
}
in.close();
sos.flush();
sos.close();
} catch(IOException e) {
out.println(e.getMessage());
}
%>
I was in difficult situation working on exporting datagrid to CSV, Excel, & PDF. The below code works for CSV & MS-Excel. Later, I will update the code for PDF format.. DataGridExporter.as package components.grid.export { import mx.collections.IViewCursor; import mx.controls.DataGrid; import mx.controls.dataGridClasses.DataGridColumn; public class DataGridExporter { private static var csvSeparator:String = “,”; private static var lineSeparator:String…
Export DataGrid to CSV
by krishna
I was in difficult situation working on exporting datagrid to CSV, Excel, & PDF. The below code works for CSV & MS-Excel.
Later, I will update the code for PDF format..
DataGridExporter.as
package components.grid.export {
import mx.collections.IViewCursor;
import mx.controls.DataGrid;
import mx.controls.dataGridClasses.DataGridColumn;
public class DataGridExporter {
private static var csvSeparator:String = “,”;
private static var lineSeparator:String = “n”;
public static function exportCSV(dg:DataGrid):String {
var columns:Array = dg.columns;
var columnCount:int = columns.length;
var headerGenerated:Boolean = false;
var dataProvider:Object = dg.dataProvider;
var rowCount:int = dataProvider.length;
var cursor:IViewCursor = dataProvider.createCursor ();
var j:int = 0;
var data:String = “”; var header:String = “”;
//loop through rows
while (!cursor.afterLast) {
var obj:Object = cursor.current;
//loop through all columns for the row
for(var k:int = 0; k < columnCount; k++) {
var column:DataGridColumn = columns[k];
//Exclude column data which is invisible (hidden)
if(!column.visible) {
continue;
} // if(!column.visible)
var colData:String = column.itemToLabel(obj);
if (colData.indexOf(“,”) > 0 )
colData = (“””+ colData +”””);
data += colData;
if(k < (columnCount -1)) {
data += csvSeparator;
} // if(k < (columnCount -1))
//generate header of CSV, only if it’s not genereted yet
if (!headerGenerated) {
header += column.headerText;
if (k < columnCount – 1) {
header += csvSeparator;
} // if (k < columnCount – 1)
} // if (!headerGenerated)
} // for(var k:int = 0; k < columnCount; k++)
headerGenerated = true;
if (j < (rowCount – 1)) {
data += lineSeparator;
} // if (j < (rowCount – 1))
j++;
cursor.moveNext ();
}
return (header + lineSeparator + data);
}
}
I was in difficult situation working on exporting datagrid to CSV, Excel, & PDF. The below code works for CSV & MS-Excel. Later, I will update the code for PDF format.. DataGridExporter.as package components.grid.export { import mx.collections.IViewCursor; import mx.controls.DataGrid; import mx.controls.dataGridClasses.DataGridColumn; public class DataGridExporter { private static var csvSeparator:String = “,”; private static var lineSeparator:String…
Leave a Reply Cancel reply
Recent Comments
Archives
- August 2025
- July 2025
- June 2025
- May 2025
- April 2025
- March 2025
- November 2024
- October 2024
- September 2024
- August 2024
- July 2024
- June 2024
- May 2024
- April 2024
- March 2024
- February 2024
- January 2024
- December 2023
- November 2023
- February 2012
- January 2012
- December 2011
- October 2011
- August 2011
- July 2011
- May 2011
- January 2011
- November 2010
- October 2010
- September 2010
- July 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- August 2008
- July 2008
- June 2008
- December 2007
- April 2007
- January 2007