Export to Excel from a repeater?

Now I use this to export the repeater (with several repeaters attached) to succeed:

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 that does not do what I want. Instead, it gives me the html in the excel file (although there is data) this is what I get (each row 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>

etc ... now the data is correct, but how can I get them to display correctly in the spreadsheet?

+5
source share
3 answers

You need to enclose all this in table tags. Excel can understand the structure of an HTML table.

Try:

Response.Write("<table>");
Response.Write(stringWrite.ToString()); 
Response.Write("</table>");
+12
source

html html body. .

0

,

, , GridView, , - :

<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>

GridView, HTML-:

<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>

-

GridView, , , gridView .

,

BTW, , , , .

: HTML - myObject

0
source

All Articles