Wednesday, February 6, 2013

Export to Excel(Concatenate the row in case of BR)

When export to excel if some columns are displayed as new row then in the exported file it show the details in new row. This export will concatenate both the column in same row.

Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AppendCookie(new HttpCookie("fileDownloadToken", exportToken.Value));
Response.AddHeader("content-disposition", "attachment;filename=ProjectResouces.xls");
string header = @" < html xmlns:x=""urn:schemas-microsoft-com:office:excel"" > 
< head >
< style >
< !--table
br {mso-data-placement:same-cell;}
tr {vertical-align:top;}
-- >
< /style >
< /head >
< body >";
Response.Write(header);

Response.Charset = "";
dgDetails.EnableViewState = false;
StringWriter sw = new StringWriter();
StringWriter headWrite = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
dgDetails.RenderControl(htw);
headWrite.Write("< table >  < /table >");
headWrite.WriteLine(sw);
Response.Write(headWrite.ToString());
string footer = "< /body> </HTML >";
Response.Write(footer);

Response.Flush();
Response.End();

No comments: