Click here to Skip to main content
14,878,193 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have a repeater in web page and did Exporting coding give below it but it is not exporting into excel .

C#
Response.Clear();
Response.AddHeader("content-disposition",
string.Format("attachment;filename={0}.xls", fileName));
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
MISRepeater.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();


Please help me if i am wrong or provide any other solution it's running well in gridview.


thanking you
mohd. wasif
Posted
Updated 13-May-11 19:14pm
v2
Comments
pankajupadhyay29 14-May-11 1:16am
   
wrap code in pre tag.

1 solution

try this code

protected void Button1_Click(object sender, EventArgs e)
       {
           Response.Clear();
           Response.Buffer = true;
           Response.AddHeader("content-disposition", "attachment;filename=finance.xls");
           Response.Charset = "";
           Response.ContentType = "application/vnd.ms-excel";

           System.IO.StringWriter stringWrite = new System.IO.StringWriter();
           System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
 //     Your Repeater Name Mine is "Rep"
           rep.RenderControl(htmlWrite);
           Response.Write("<table>");
           Response.Write(stringWrite.ToString());
           Response.Write("</table>");
           Response.End();
       }
   
Comments
Mayank Pathak, Chandigarh 12-Jul-12 11:46am
   
Very Nice, and helpful budddy...works in one go...
Je P 11-Jun-13 6:21am
   
I have used same code to generate .xls file,
now i am trying to read this generated file using OLEDB ADO.NET (Microsoft.Jet.OLEDB.4.0;) it gives error -"External table is not in the expected format"

Please guide how to read file using OLEDB ADO.NET.
James King 4-Aug-14 6:01am
   
Perfect. I didn't think it would be that simple!
Zalak Artist 21-Nov-14 1:22am
   
Repeater having hiddenfield contorl do not work for this code.
Please inform if there is a solution to export a repeater (that has hidden field control ) to excel format

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