AS3 (Flex) code (taken from WiredWizard Web Design blog):
//Filename: Test_AS32XLS.mxml
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.core.UIComponent;
import mx.core.Container;
import mx.events.ItemClickEvent;
import mx.utils.ObjectProxy;
import flash.errors.*;
import flash.events.*;
import flash.external.*;
import flash.net.URLLoader;
import flash.net.URLVariables;
import flash.net.URLRequest;
public var urlExcelExport:String = "here you need to write your url reference to SaveToXLFile.aspx";
[Bindable]
private var initialData:ArrayCollection;
private function initApp():void {
initialData = new ArrayCollection([{first:'aaa', last:'zzz', email:'email1@email.com'},
{first:'bbb', last:'yyy', email:'email2@email2.com'}]);
}
private function loadDGInExcel():void {
var variables:URLVariables = new URLVariables();
variables.XLtabletoconvert = convertDGToHTMLTable();
var u:URLRequest = new URLRequest(urlExcelExport);
u.data = variables;
u.method = URLRequestMethod.POST;
navigateToURL(u,"_self");
}
private function convertDGToHTMLTable():String {
dg.setStyle("headerColor","#33FFFF");
dg.setStyle("themeColor","#33FF00");
var font:String = dg.getStyle('fontFamily');
var size:String = dg.getStyle('fontSize');
var str:String = '';
var colors:String = '';
var style:String = 'style="font-family:'+font+';font-size:' + size + 'pt;"';
var hcolor:Array;
if(dg.getStyle("headerColor") != undefined) {
hcolor = [dg.getStyle("headerColor")];
} else {
hcolor = dg.getStyle("headerColors");
}
str+= '';
for(var i:int = 0;i colors = dg.getStyle("themeColor");
if(dg.columns[i].headerText != undefined) {
str+=" ";
} else {
str+= "";
}
}
str += "";
colors = dg.getStyle("alternatingRowColors");
for(var j:int =0; j str+=" ";
for(var k:int=0; k < dg.columns.length; k++) {
if(dg.dataProvider.getItemAt(j) != undefined && dg.dataProvider.getItemAt(j) != null) {
if(dg.columns[k].labelFunction != undefined) {
str += "";
} else {
str += "";
}
}
}
str += "";
}
str+="
"+dg.columns[i].headerText+" "+dg.columns[i].dataField+"
"+dg.columns[k].labelFunction( dg.dataProvider.getItemAt(j), dg.columns[k].dataField) + "
" + dg.dataProvider.getItemAt(j)[dg.columns[k].dataField] + "
";
return str;
}
]]>
C# code:
// Filename: SaveToXLFile.aspx.cs
public partial class SaveToXLFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Change the response headers to output a Temporal XL File
Response.Clear();
Response.ContentType = "application/ms-excel";
Response.AddHeader("content-disposition", "attachment;filename=myfilename.xls");
// Get the variable and write it to the Response object
String x = Request["XLtabletoconvert"];
Response.Write(x);
Response.Flush();
Response.End();
}
}
Also, due to .NET Framework validation of Request object not to get cross-coding, you'll need to add in your SaveToXLFile.aspx the following directive:
<%@ Page ValidateRequest="false" ...