因为数据量很大,导出Excel时报错,所以改用导出为csv文件
代码如下:
public static function exportAVDToCSV(dg:AdvancedDataGrid):void{
var k:int;
var j:int;
var dataProviderCollection:ArrayCollection = dg.dataProvider as ArrayCollection;
var rowCount:int = dataProviderCollection.length;
var ExcelStr:Array = [];//保存表格所有行
var columnsFiledText:Array = new Array();//列标题
//Set the tableheader data (retrieves information from the datagrid header
for(var i:int = 0;i<dg.columns.length;i++) {
if(dg.columns[i].headerText != undefined) {
columnsFiledText.push(dg.columns[i].headerText);
} else {
columnsFiledText.push(dg.columns[i].dataField);
}
}
//保存标题行
ExcelStr.push(columnsFiledText.join(",").toString());
for(var r:int = 0;r<rowCount;r++) {
var rowData:Object = dataProviderCollection.getItemAt(r);
// Alert.show("rowCount"+rowCount.toString()+"dg.columns.length:"+dg.columns.length.toString());
var csvRow:Array =new Array();//定义一行数据
for(k = 0; k < dg.columns.length; k++) {
// Alert.show(dg.columns.length.toString());
var columnsFiledName:Array = new Array();//列数据字段
if(rowData != undefined && rowData != null) {
if(dg.columns[k].labelFunction != undefined) {
columnsFiledName.push(dg.columns[k].labelFunction(rowData,dg.columns[k].dataField));
} else {
columnsFiledName.push(rowData[dg.columns[k].dataField]);
}
}
// Alert.show("第"+r+"行"+"第"+k+"列"+columnsFiledName.toString());
trace(columnsFiledName);
csvRow.push(columnsFiledName.join(",").toString());
}
ExcelStr.push(csvRow.join(",").toString());
}
var bytes:ByteArray = new ByteArray();
var fileReference:FileReference = new FileReference();
bytes.writeMultiByte(ExcelStr.join("\r\n"),"gb2312");
fileReference.save(bytes,"test.csv");
}
代码如下:
public static function exportAVDToCSV(dg:AdvancedDataGrid):void{
var k:int;
var j:int;
var dataProviderCollection:ArrayCollection = dg.dataProvider as ArrayCollection;
var rowCount:int = dataProviderCollection.length;
var ExcelStr:Array = [];//保存表格所有行
var columnsFiledText:Array = new Array();//列标题
//Set the tableheader data (retrieves information from the datagrid header
for(var i:int = 0;i<dg.columns.length;i++) {
if(dg.columns[i].headerText != undefined) {
columnsFiledText.push(dg.columns[i].headerText);
} else {
columnsFiledText.push(dg.columns[i].dataField);
}
}
//保存标题行
ExcelStr.push(columnsFiledText.join(",").toString());
for(var r:int = 0;r<rowCount;r++) {
var rowData:Object = dataProviderCollection.getItemAt(r);
// Alert.show("rowCount"+rowCount.toString()+"dg.columns.length:"+dg.columns.length.toString());
var csvRow:Array =new Array();//定义一行数据
for(k = 0; k < dg.columns.length; k++) {
// Alert.show(dg.columns.length.toString());
var columnsFiledName:Array = new Array();//列数据字段
if(rowData != undefined && rowData != null) {
if(dg.columns[k].labelFunction != undefined) {
columnsFiledName.push(dg.columns[k].labelFunction(rowData,dg.columns[k].dataField));
} else {
columnsFiledName.push(rowData[dg.columns[k].dataField]);
}
}
// Alert.show("第"+r+"行"+"第"+k+"列"+columnsFiledName.toString());
trace(columnsFiledName);
csvRow.push(columnsFiledName.join(",").toString());
}
ExcelStr.push(csvRow.join(",").toString());
}
var bytes:ByteArray = new ByteArray();
var fileReference:FileReference = new FileReference();
bytes.writeMultiByte(ExcelStr.join("\r\n"),"gb2312");
fileReference.save(bytes,"test.csv");
}