Wednesday, September 30, 2009

Project Report On New Business

Exporting a DataGrid to Excel using AS3 and. NET

I haven't found on the Internet any complete solution for this problem using C# as the calling page receiving the Request object, so that's why I post it here. Hope this helps.



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 was urlExcelExport: String = "here you need to write your url reference two SaveToXLFile.aspx";

[Vol Able]
private the initial data: 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 ("header color", "# 33FFFF");
dg.setStyle ("dem color", "# 33FF00 ");

was font: String = dg.getStyle ('fontFamily');
were size: String = dg.getStyle ('fontsize');
was str: String ='';
was Colors: String = ' ';
was style: String =' style = "font-family: '+ font +'; font-size: '+ size +' pt;" ';
was hcolor: Array;

if (dg.getStyle ("headerColor")! = Undefined) {
hcolor = [dg.getStyle ("headerColor")];
} else {
hcolor dg.getStyle = ("headerColors ");}


str + = '';

for (var i int = 0; in
colors dg.getStyle = (" Them color ");

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" ...


0 comments:

Post a Comment