Exporting dataset in Excel format with just 4lines

Well guys I wanted to have my dataset in an excel format, and i was dealing with quite a messed up code using CR9.. Is there really a neeed when it’s just a matter of writing ds contents into an excel format..>? hell NO~ no neeed to make the application loaded with objects, when you can simply have them it all in 4 lines… no reference and no mess upp*

here goes the main call:

this.DataSetConvert(ds, Response, path);

and herez the method definition :o-

private void DataSetConvert(DataSet ds, System.Web.HttpResponse response, String filePath) {

try {

string strHeading =Request.QueryString[“txtReportHeading1”];response.Clear();

response.Charset = “”;

response.ContentType = “application/vnd.ms-excel”;

System.IO.StringWriter stringWrite = new System.IO.StringWriter();System.IO.StreamWriter streamWrite = new System.IO.StreamWriter(filePath); //To Wirte in File

System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid();dg.Caption=”<BR><B>”+strHeading+”</B><BR><BR>” + “<B> Report Date: </B>” + DateTime.Now.ToShortDateString() + “<BR><BR>”;

dg.HeaderStyle.Font.Bold=true;dg.HeaderStyle.BackColor=System.Drawing.Color.Navy;

dg.HeaderStyle.ForeColor=System.Drawing.Color.White;

dg.CaptionAlign=System.Web.UI.WebControls.TableCaptionAlign.Left;

dg.DataSource = ds.Tables[0];

dg.DataBind();

dg.RenderControl(htmlWrite);

streamWrite.Write(stringWrite.ToString());

streamWrite.Close();

response.Write(stringWrite.ToString());

response.End();

}

catch (Exception ex){

throw ex;}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s