Como guardar datos desde un Componente DataGrid en un archivo CVS compatible con Excel.
Objetivo: Dado un componente DataGrid lleno con datos lograr exportarlo en un archivo externo en ActionScript 3. Después de un arduo trabajo de investigación y busqueda en varios sitios web les dejo publicado lo que logré.En un archivo nuevo AS3, en la escena colocar un componente datagrid llamando aDg y un componente TextInput, en una capa de acciones colocar el siguiente código:
import fl.data.DataProvider;
import flash.events.MouseEvent;
import fl.data.DataProvider;
import fl.controls.DataGrid;
import fl.controls.Button;
import fl.controls.dataGridClasses.DataGridColumn;
//Carga automatica de datos en el DataGrid--------------------------------
bldRosterGrid(aDg);
var aRoster:Array = new Array();
aRoster = [
{Name:"Wilma Carter", Bats:"R", Throws:"R", Year:"So", Home: "Redlands, CA"},
{Name:"Sue Pennypacker", Bats:"L", Throws:"R", Year:"Fr", Home: "Athens, GA"},
{Name:"Jill Smithfield", Bats:"R", Throws:"L", Year:"Sr", Home: "Spokane, WA"},
{Name:"Shirley Goth", Bats:"R", Throws:"R", Year:"Sr", Home: "Carson, NV"},
{Name:"Jennifer Dunbar", Bats:"R", Throws:"R", Year:"Fr", Home: "Seaside, CA"},
{Name:"Patty Crawford", Bats:"L", Throws:"L", Year:"Jr", Home: "Whittier, CA"},
{Name:"Angelina Davis", Bats:"R", Throws:"R", Year:"So", Home: "Odessa, TX"},
{Name:"Maria Santiago", Bats:"L", Throws:"L", Year:"Sr", Home: "Tacoma, WA"},
{Name:"Debbie Ferguson", Bats:"R", Throws:"R", Year: "Jr", Home: "Bend, OR"},
{Name:"Karen Bronson", Bats:"R", Throws:"R", Year: "Sr", Home: "Billings, MO"},
{Name:"Sylvia Munson", Bats:"R", Throws:"R", Year: "Jr", Home: "Pasadena, CA"},
{Name:"Carla Gomez", Bats:"R", Throws:"L", Year: "Sr", Home: "Corona, CA"},
{Name:"Betty Kay", Bats:"R", Throws:"R", Year: "Fr", Home: "Palo Alto, CA"},
];
aDg.dataProvider = new DataProvider(aRoster);
aDg.rowCount = aDg.length;
function bldRosterGrid(dg:DataGrid){
dg.setSize(400, 300);
dg.columns = ["Name", "Bats", "Throws", "Year", "Home"];
dg.columns[0].width = 120;
dg.columns[1].width = 50;
dg.columns[2].width = 50;
dg.columns[3].width = 40;
dg.columns[4].width = 120;
dg.move(50,50);
};
//------- EXPORTAR LOS DATOS A CSV ----------------------------------------------------------//
function exportCSV(dg:DataGrid):String {
//var csvSeparator:String="\t";
var csvSeparator:String=";";
var lineSeparator:String="\n";
var data:String = "";
var columns:Array = dg.columns; // columnas del dataGrid
var columnCount:int = columns.length;
var column:DataGridColumn;
var header:String = "";
var headerGenerated:Boolean = false;
var dataProvider:DataProvider = dg.dataProvider;
var rowCount:int = dataProvider.length; // cantidad de filas
var dp:Object = null;
var j:int = 0;
//loop through rows
while (j
obj = j;
//loop through all columns for the row
for (var k:int = 0; k < columnCount; k++) {
column = columns[k];
//Exclude column data which is invisible (hidden)
if (!column.visible) { continue; };
data += "\""+ column.itemToLabel(dg.getItemAt(j))+ "\"";
trace(column.itemToLabel(dg.getItemAt(j))+"—-"+ data);
var menos = columnCount-1 ;
if (k < menos) { data += csvSeparator; };
//generate header of CSV, only if it’s not genereted yet
if (!headerGenerated) {
header += "\"" + column.headerText + "\"";
if (k < menos) { header += csvSeparator; };
};
};
headerGenerated = true;
var filamenos = rowCount-1;
if (j < filamenos) { data += lineSeparator; };
j++;
//cursor.moveNext();
}
//set references to null:
dataProvider = null;
columns = null;
column = null;
return (header + "\r\n" + data);
};
//exportCSV(aDg); test con el trace de salida
/*
//Test con boton y trace de salida.
saveBtn3.addEventListener(MouseEvent.CLICK, fcEXPORTAR);
function fcEXPORTAR(MouseEvent){ exportCSV(aDg); }
*/
//--------------GUARDA LISTADO ---------------
import flash.net.FileReference;
saveBtn3.addEventListener(MouseEvent.CLICK, writeCSV);
function writeCSV(event:MouseEvent) {
var csv:String = exportCSV(aDg);
var file:FileReference = new FileReference();
var bytes:ByteArray = new ByteArray();
bytes.writeUTFBytes(csv);
file.save(bytes,'datos.csv');
}
DESCARGA DEL ARCHIVO FLA CS5.5
REFERENCIA Y AGRADECIMIENTO A:
http://stackoverflow.com/questions/7316416/writing-to-csv-file-in-as3http://www.abdulqabiz.com/blog/archives/2007/08/03/datagriddataexporter-export-datagrid-data-as-csv/
http://jasmeetsingh.wordpress.com/tag/datagrid-to-csv/
http://onyxmueller.net/2011/08/15/advanceddatagrid-csv-export-utility-class/