从c#.net导出到Excel

时间:2008-12-18 17:15:48

标签: c# excel export

从c#.net导出 我遇到了一个问题我有一个表单,当我导出excel导致excel任何想法为什么会发生这种情况我包括下面的ASP代码。

<%@ Page Language="C#" MasterPageFile="~/masterpages/Admin.master" AutoEventWireup="true" CodeFile="members-search-adv.aspx.cs" Inherits="masteradmin_members_search_adv" Title="LISA - Life Insurance Settlement" %>
<%@ Register TagPrefix="UC" TagName="Paging" Src="~/controls/Paging.ascx" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <script type="text/javascript" src="sorttable.js"></script>
 Go to <a href="members-search.aspx" class="link">Quick Search</a>
<h1>Contact Database - Advanced Search</h1>
<asp:Label ID="lblSearchCriteria" runat="server" Text=""  ForeColor="blue"></asp:Label>
<table width="100%" cellspacing="0" cellpadding="0">
    <tr>
        <td valign="top" width="50%">
<table border="0" cellpadding="3" cellspacing="1" style="height: 214px; width: 101%;">  
    <tr>
        <td class="form1" align="right"><strong>OpenSearch:</strong></td>
        <td class="form2">
            <asp:TextBox ID="txtSearch" runat="server" CssClass="fields" Width="245px" ></asp:TextBox><b>*</b>&nbsp;<br><b>*</b><small>Search By Company Name, First Name, Last Name, Tags, Comments</small></td>
    </tr>
    <tr>
        <td class="form1" align="right"><strong>Industry Segment:</strong></td>
        <td class="form2"><asp:DropDownList ID="ddlIndSegment" runat="server" CssClass="fields" /></td>
    </tr>
    <tr>
        <td class="form1" align="right"><strong>Member Type:</strong></td>
        <td class="form2"><asp:DropDownList ID="ddlMemberType" runat="server" CssClass="fields" /></td>
    </tr>
    <tr>
        <td class="form1" align="right"><strong>Member Rep only:</strong></td>
        <td class="form2"><asp:CheckBox ID="cbxMemberRep" runat="server" /></td>
    </tr>
    <tr>
        <td class="form1" align="right"><strong>Board Only:</strong></td>
        <td class="form2"><asp:CheckBox ID="cbxBoardOnly" runat="server" /></td>
    </tr>
    <tr>
        <td colspan="2" align="right">
            <asp:ImageButton ID="btnSearch" runat="server" ImageUrl="/RadControls/images/bu_search.jpg" />
        </td>
    </tr>
    </table>
    </td>
        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
    <td valign="top" width="50%">
    <%--split the table here--%>
        <table border="0" cellpadding="3" cellspacing="1" 
            style="width: 98%; margin-left: 0px;">
    <tr>
        <td class="form1" align="right"><strong>Suspended Only:</strong></td>
        <td class="form2"><asp:CheckBox ID="cbxSuspended" runat="server" /></td>
    </tr>
    <tr>
        <td class="form1" align="right"><strong>Active Only:</strong></td>
        <td class="form2"><asp:CheckBox ID="cbxActiveOnly" runat="server" /></td>
    </tr>
    <tr>
        <td class="form1" align="right"><strong>Allow Other Members To See My Info Only:</strong></td>
        <td class="form2"><asp:CheckBox ID="cbxAllowOtherMembers" runat="server" /></td>
    </tr>
    <tr>
        <td class="form1" align="right"><strong>State:</strong></td>
        <td class="form2"><asp:DropDownList ID="ddlState" runat="server" CssClass="fields" /></td>
    </tr>
        <tr>
        <td class="form1" align="right"><strong>By Special Level:</strong></td>
        <td class="form2"><asp:DropDownList ID="ddlSpecialLevel" runat="server" CssClass="fields" /></td>
    </tr>

    <tr>
        <td class="form1" align="right"><strong>Sort by:</strong></td>
        <td class="form2">
            <asp:DropDownList ID="ddlSortBy" runat="server" CssClass="fields">
                <asp:ListItem Value="CompanyName">Company</asp:ListItem>    
                <asp:ListItem Value="FirstName">First Name</asp:ListItem>
                <asp:ListItem Value="LastName">Last Name</asp:ListItem>
                <asp:ListItem Value="MemberCategoryId">Membership Type</asp:ListItem>
                <asp:ListItem Value="IndustrySegmentId">Industry Segment</asp:ListItem>
            </asp:DropDownList>
        </td>
    </tr>

</table>    
</td>
</tr>
</table>
    <table  border="0" cellpadding="3" cellspacing="1" width="100%">        
<tr>
<td><asp:Literal ID="litTotalCount" runat="server" /></td><td align=left class="form3">
    <asp:Button ID="Button1" runat="server"  onclick="Button1_Click" Text="Button" />
    <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Export To Excel</asp:LinkButton>
    <small>* Click on the column to SORT</small></td></tr>
</table>

    <asp:Repeater ID="rptList" runat="server">
        <HeaderTemplate>
        <table class="sortable" border="0" cellpadding="3" cellspacing="1" width="100%">        
            <tr>
                <td align="left" valign="top" class="form1"><b>Company</b></td>
                <td align="left" valign="top" class="form1"><b>First Name</b></td>
                <td align="left" valign="top" class="form1"><b>Last Name</b></td>
                <td align="left" valign="top" class="form1"><b>Email</b></td>
            </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td class="form2" align="left" valign="top"><asp:HyperLink ID="hlCompany" runat="server" CssClass="link"></asp:HyperLink></td>
                <td class="form2" align="left" valign="top"><asp:HyperLink ID="hlFirstName" runat="server" CssClass="link"></asp:HyperLink></td>
                <td class="form2" align="left" valign="top"><asp:HyperLink ID="hlLastName" runat="server" CssClass="link"></asp:HyperLink></td>
                <td class="form2" align="left" valign="top"><asp:HyperLink ID="hlEmail" runat="server" CssClass="link"></asp:HyperLink> </td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
        </table>
        </FooterTemplate>
    </asp:Repeater>

    <asp:GridView ID="GridView2" runat="server">
    </asp:GridView>

    <asp:PlaceHolder ID="PlaceHolder1" runat="server"><br />
    <UC:Paging Id="ctlPaging" runat="server" />
    </asp:PlaceHolder>
<%--    <div align="center">
        <br /><UC:Paging Id="ctlPaging" runat="server" />
    </div>--%>

</asp:Content>

抱歉,这是对此问题的更好解释 从c#.net导出我遇到了问题我有一个表单,当我导出到excel时我得到以下<div></div>我在数据网格中有数据但在excel中没有我只是得到<div></div>


这是我正在使用的功能

void ExportToExcel3()
 {
     Response.Clear();
     Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
     Response.Charset = "";
     Response.ContentType = "application/vnd.xls";
     System.IO.StringWriter stringWrite = new System.IO.StringWriter();
     System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
     GridView2.RenderControl(htmlWrite);
     Response.Write(stringWrite.ToString());
     Response.End();
 }



public override void VerifyRenderingInServerForm(Control control)
 {

 }

我在导出之前填充数据网格。这是代码行。有趣的是,我只使用这部分代码创建了一个新的webform,并且导出工作正常。 您认为我在页面中导致冲突的datarepeater存在问题。

MemberList list = MemberDB.GetMembers(sql, m_page, m_RecordPerPage, out count, _state);
          this.GridView2.DataSource = list;
          this.GridView2.DataBind();

2 个答案:

答案 0 :(得分:1)

这根本不是顺序 - 它不是asp.net和网络的工作方式。

在加载阶段,您没有填充网格:检索数据并将其设置为表的数据源,但网格(实际上是转发器)尚未填充。直到数据绑定阶段才会发生这种情况。在数据绑定阶段之后(在呈现阶段期间),具有数据中继转发器的页面最终由服务器发送到浏览器。此时所有服务器端的东西都被处理掉了。

现在,浏览器最终可以显示带有“导出到Excel”按钮的页面,用户可以单击该按钮。再说一遍:服务器已经在破坏你工作中难以构建的所有数据。当用户点击您的按钮时,会创建一个全新的请求,并将一个全新的页面发送回浏览器。您必须再次将所有数据加载到网格中。

答案 1 :(得分:-1)

我找到了解决问题的方法。谢谢大家的贡献。在Load_Page事件中,我在if!postback内部进行了绑定。