Right now I am using this to export a repeater (with multiple nested repeaters) to excel:
protected void ExportToExcel(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);
rptMinistry.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
But this is not doing what I want. Instead it is giving me html in the excel file (though the data is there) this is what I get (each line is a cell in the excel sheet):
<tr class="alt">
<td class='hidden'>LOR In Development</td>
<td>MOD</td>
<td>Air Force</td>
<td class="size1"></td>
<td>Hellfire (AGM-114) Follow On</td>
<td>10-Mar-08</td>
<td class="align_right ">$50,000,000.00</td>
<td class="align_right hidden">$0.00</td>
</tr>
<tr class="alt">
<td class='hidden'>LOR In Development</td>
<td>MOD</td>
<td>Air Force</td>
<td class="size1"></td>
<td>Precision Strike Mi-17 (block 20)</td>
<td>17-May-08</td>
<td class="align_right ">$20,100,000.00</td>
<td class="align_right hidden">$0.00</td>
</tr>
and so on... now the data is correct, but how can I get it to show up correctly in the spreadsheet?
You need to enclose all of that in table tags. Excel can understand HTML table structures.
Try:
not to answer your question directly, but given you my opinion
for that kinda of data, is in my opinion that you should use a GridView control, taking your example you will need to write something like:
using a GridView all ou nee dto write in the HTML part is only:
something simpler and easier to read
you will have much more control using a GridView object rather than a Repeater, and, you will never have that kinda of problems, because rendering the gridView will always came with the table tags.
Hope it helps
And BTW, I tested your case and i did not got any problems even if I was not writting the tags like Spencer mention.
to see my code: File with HTML and Method - File with myObject
you should make the output file a proper html file, with the html and body tags. That should work better.