Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export to Excel from a Repeater?

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?

like image 710
naspinski Avatar asked Jan 13 '09 07:01

naspinski


2 Answers

You need to enclose all of that in table tags. Excel can understand HTML table structures.

Try:

Response.Write("<table>");
Response.Write(stringWrite.ToString()); 
Response.Write("</table>");
like image 91
Spencer Ruport Avatar answered Oct 30 '22 23:10

Spencer Ruport


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:

<asp:Repeater ID="rpt" runat="server" DataSourceID="ods">
    <HeaderTemplate>
        <table>
        <tr>
            <td>Header</td>
            <td>Type</td>
            <td>Name</td>
            <td>Date</td>
            <td>Amount</td>
        </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <%#Eval("Header")%>
            </td>
            <td>
                <%#Eval("Type")%>
            </td>
            <td>
                <%#Eval("Name")%>
            </td>
            <td>
                <%#Eval("Date", "{0:d}")%>
            </td>
            <td>
                <%#Eval("Value", "{0:c}")%>
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>

using a GridView all ou nee dto write in the HTML part is only:

<asp:GridView ID="gv" runat="server" DataSourceID="ods" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Header" HeaderText="Header" />
        <asp:BoundField DataField="Type" HeaderText="Type" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Date" DataFormatString="{0:d}" HeaderText="Date" />
        <asp:BoundField DataField="Value" DataFormatString="{0:c}" HeaderText="Value" />
    </Columns>
</asp:GridView>

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

like image 41
balexandre Avatar answered Oct 31 '22 01:10

balexandre