Flex Excel export function implementation

Source: Internet
Author: User
Tags rowcount setcell

Method 1:

1. Excel export code:

Try
{
VaR Bytes: bytearray = new bytearray ();
Bytes. writemultibyte (maid. exportscv (DataGrid), "CN-GB ");
VaR fr: filereference = new filereference ();
VaR Date: Date = new date ();
VaR datetime: String = date. fullyear. tostring () + "year" + (number (date. month) + 1 ). tostring () + "month" + date. date. tostring () + "day ";
VaR excelname: String = datetime + filename;

Fr. Save (bytes, excelname );
}
Catch (E: Error)
{
Mapuni. errorwindow (E. tostring ());
}

 

2. The called class datagridexporter:

Public class datagridexporter
{
Public Function datagridexporter ()
{

}

Public static function exportscv (DG: DataGrid, csvseparator: String = "\ t", lineseparator: String = "\ n"): String
{
VaR datastr: String = "";
VaR columns: array = DG. columns;
VaR columncount: Int = columns. length;
VaR column: datagridcolumn;
VaR header: String = "";
VaR headergenerated: Boolean = false;
VaR dataprovider: Object = DG. dataprovider;
VaR rowcount: Int = dataprovider. length;
VaR DP: Object = NULL;
VaR cursor: iviewcursor = dataprovider. createcursor ();

VaR J: Int = 0;

While (! Cursor. afterlast)
{
VaR OBJ: Object = NULL;
OBJ = cursor. Current;
For (var k: Int = 0; k <columncount; k ++)
{
Column = columns [k];
If (! Column. Visible)
{
Continue;
}
Datastr + = "\" "+ column. itemtolabel (OBJ) + "\"";
If (k <(columnCount-1 ))
{
Datastr + = csvseparator;

}
If (! Headergenerated)
{
Header + = "\" "+ column. headertext + "\"";
If (k <(columnCount-1 ))
{
Header + = csvseparator;
}

}
}
Headergenerated = true;

If (j <(rowCount-1 ))
{
Datastr + = lineseparator;
}
J ++;
Cursor. movenext ();
}

Dataprovider = NULL;
Columns = NULL;
Column = NULL;
Return (header + "\ r \ n" + datastr );

}

Method 2:

1. var excelfactory: saveexcelfield = new saveexcelfield ();

Excelfactory. exporttoexcel (DataGrid, "Henderson text", true );

Public Function exporttoexcel (DataGrid: DataGrid, filename: String, alldata: Boolean): void
{
Try
{
VaR fields: array = new array ();
VaR sheet: sheet = new sheet ();
VaR dataprovidercollection: arraycollection = DataGrid. dataprovider as arraycollection;


VaR I: Int = 0;
VaR columns: array = DataGrid. columns;


VaR rowcount: Int = dataprovidercollection. length;

If (alldata)
{
Sheet. Resize (rowcount + 1, DataGrid. columncount );

Columns. Pop ();


}
Else
{
Sheet. Resize (rowcount + 1, DataGrid. columncount );

/*
For (var m: Int = 0; m <columns. length; m ++)
{
VaR field1: datagridcolumn = columns [m] As datagridcolumn;
// Var field: Maid in Columns
Fields. Push (field1.datafield. tostring ());
Sheet. setcell (0, I, field1.headertext. tostring ());
I ++;
}*/

}
// Sheet. Resize (rowcount + 1, DataGrid. columnCount-1 );

For each (VAR field: datagridcolumn in columns)
{
Fields. Push (field. datafield. tostring ());
Sheet. setcell (0, I, field. headertext. tostring ());
I ++;
}




For (var r: Int = 0; r <rowcount; r ++)
{
VaR record: Object = dataprovidercollection. getitemat (R );
Insertrecordinsheet (R + 1, sheet, record, fields );
}
VaR XLS: excelfile = new excelfile ();
XLS. Sheets. additem (sheet );

VaR Bytes: bytearray = XLS. savetobytearray ();
VaR fr: filereference = new filereference ();

Fr. Save (bytes, filename );
}
Catch (E: Error)
{
Mapuni. errorwindow (E. tostring ());
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.