C# 从中继器导出到 Excel?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/438184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 03:23:30  来源:igfitidea点击:

Export to Excel from a Repeater?

c#asp.netexcelrepeater

提问by naspinski

Right now I am using this to export a repeater (with multiple nested repeaters) to excel:

现在我正在使用它来导出一个转发器(带有多个嵌套转发器)到 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):

但这不是我想要的。相反,它在 excel 文件中给了我 html(尽管数据在那里),这就是我得到的(每一行都是 Excel 工作表中的一个单元格):

<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 ">,000,000.00</td>
 <td class="align_right hidden">
Response.Write("<table>");
Response.Write(stringWrite.ToString()); 
Response.Write("</table>");
.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 ">,100,000.00</td> <td class="align_right hidden">
<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>
.00</td> </tr>

and so on... now the data is correct, but how can I get it to show up correctly in the spreadsheet?

等等......现在数据是正确的,但我怎样才能让它在电子表格中正确显示?

采纳答案by Spencer Ruport

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

您需要将所有这些都包含在表格标签中。Excel 可以理解 HTML 表格结构。

Try:

尝试:

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

回答by Kobi

you should make the output file a proper html file, with the html and body tags. That should work better.

您应该使用 html 和 body 标签使输出文件成为正确的 html 文件。那应该效果更好。

回答by balexandre

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:

对于那种数据,我认为您应该使用 GridView 控件,以您的示例为例,您需要编写如下内容:

##代码##

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

使用 GridView 只需要在 HTML 部分写入:

##代码##

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.

使用 GridView 对象而不是 Repeater,您将有更多的控制权,而且您永远不会遇到那种问题,因为渲染 gridView 将始终与 table 标签一起出现。

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

查看我的代码:带有 HTML 和方法的文件-带有 myObject 的文件