mostlylucid

STATIC ARCHIVE of mostlylucid.co.uk of old
posts - 916, comments - 758, trackbacks - 11

My Links

News

Archives

Post Categories

Misc. Coding

Dumb little thing...class which outputs an Excel compatible table from a DataSet

Very simple and based on the one I blogged about previously (well not based on the code but based on the same idea). Anyway, hook this up to a link button, pass in a dataset and it'll provide a downloadable Excel compatible file...

public class GetExcel

{

public static void Convert(DataSet ds, int dataTableId, HttpResponse response, string fileName)

{

DataGrid dg = new DataGrid();

dg.DataSource = ds.Tables[dataTableId];

dg.DataBind();

response.Buffer = true;

response.ContentType="application/vnd.ms-excel";

response.AddHeader("Content-Disposition", "attachment;filename=" + fileName) ;

using(StringWriter sw = new StringWriter())

{

using(HtmlTextWriter writer = new HtmlTextWriter(sw))

{

dg.RenderControl(writer);

response.Write(sw.ToString());

}

}

response.End();

}

public static void Convert(DataSet ds, int dataTableId, HttpResponse response)

{

Convert(ds,dataTableId,response,"report.xls");

}

public static void Convert(DataSet ds, HttpResponse response)

{

Convert(ds,0,response);

}

}

Print | posted on Wednesday, March 03, 2004 10:41 AM | Filed Under [ ASP.NET Code Snippets ]

Feedback

# re: Dumb little thing...class which outputs an Excel compatible table from a DataSet

Really interesting routine Scott... I'm curious to try it. Thanks!
3/3/2004 11:15 AM | Stefano Demiliani

# Take Outs for 3 March 2004

Take Outs for 3 March 2004
3/4/2004 4:15 AM | Enjoy Every Sandwich
Comments have been closed on this topic.

Powered by: