0
Reply

How to add user defined Header in excel report

Sourabh Sharma

Sourabh Sharma

Apr 15 2008 12:49 AM
2.3k

Hi Friends,

I am using C# language,I am getting my data in Excel sheet for reporting purpose from database but i want to know how to put the company name or any other header into sheet.

Do you have any idea...Plz help.

thanks.

Format: 

Headername

Comapany name

Headername

Fieldname1

Fieldname2

Fieldname3

Value1

Value2

Value3

 Here

Fieldname and Value are coming from database.I want to put

Comapany name

Headername

  according to me at my location watever location I pass...what code should i added to my code

 Code:

public static void ExportDataSetToExcel(DataTable ds, string filename)

{

string path = ConfigurationManager.AppSettings[0];

StringWriter Writer = new StringWriter();

filename = path + "\\" + filename +".xls";

StreamWriter streamWriter = new StreamWriter(filename);

HttpResponse response = new HttpResponse(Writer);

// first let's clean up the response.object

response.Clear();

response.Charset = "";

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

using (StringWriter sw = new StringWriter())

{

using (System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw))

{

System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid();

dg.DataSource = ds;

dg.DataBind();

dg.RenderControl(htw);

response.Write(sw.ToString());

streamWriter.Write(sw.ToString());

streamWriter.Close();

}

}

I am calling this code into my main function