Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How Add new Row in Excel when we export gridview to excel pllz help me
Posted
Comments
[no name] 18-Dec-12 0:49am    
I am afraid you have to add a new row in your gridview and then, export to Excel, or add a new row dircetly in yo9ur excel file after export
Mandy Nagra 18-Dec-12 3:11am    
drictly in excl sheet
CandyT 18-Dec-12 22:33pm    
You could add a new row after it is exported into excel

1 solution

Follow the below steps
1. Render GridView control to HTMLTextWriter
2. Copy TextWriter content to a StringBuilder variable
3. Prepare a html table row (<table><tbody><tr></tr></tbody></table>) with the values for the new row. Each column value should be within separate <table><tbody><tr><td></td></tr></tbody></table>
4. Using StringBuilder variable, insert the above row to the html just before end table tag ()
5. Now write the content using Response.Write()
6. This will download an excel with your new row

Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Test.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

GridView1.RenderControl(htmlWrite);


StringBuilder sb = new StringBuilder(stringWrite.ToString());
sb = sb.Replace("</table>", "<tr><td>Column1</td><td>Column2</td></tr></table>");

Response.Write(sb);
Response.End();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900