从c#.net导出到Excel

时间:2021-05-24 23:17:20

Exporting from c#.net I am getting a problem I have a form that when I export to excel as a result in excel Any ideas why is this happening I am including the ASP code below.

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

Sorry but this is a better explanation of the issue Exporting from c#.net I am getting a problem I have a form that when I export to excel I ge the following <div></div> I have data in the datagrid but not in excel I am just getting <div></div>

对不起,但这是一个更好的解释问题从c#.net导出我遇到问题我有一个表单,当我导出到Excel时我ge以下

我有数据网格中的数据但不是excel我刚刚得到


And this is the function that I am using

这是我正在使用的功能

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

 }

I am populating the datagrid prior to the export. This is the line of code. It's funny I create a new webform just with this section of code and the export worked. Do you think there's a problem with the datarepeater that I have in the page that is causing the conflict.

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

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

2 个解决方案

#1


That's not the sequence at all- it's just not how asp.net and the web work.

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

You don't fill the grid during the load phase: you retrieve the data and set it as the datasource for the table, but the grid (actually a repeater) isn't filled yet. That doesn't happen until the databinding phase. After the databinding phase (during the render phase) the page with the databound repeater is finally sent by the server to the browser. At this point all your server-side stuff is disposed.

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

Now the browser can finally show the page with the big "Export to Excel" button which the user can click. Again: the server is already destroying all the data you worked so hard to build. When the user clicks your button, a brand new request is created and a brand new page will be sent back to the browser. You have to load all your data into the grid again.

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

#2


I found the solution to my problem. Thank you all for contributing. On the Load_Page Event I had the binding inside of an if !postback.

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

#1


That's not the sequence at all- it's just not how asp.net and the web work.

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

You don't fill the grid during the load phase: you retrieve the data and set it as the datasource for the table, but the grid (actually a repeater) isn't filled yet. That doesn't happen until the databinding phase. After the databinding phase (during the render phase) the page with the databound repeater is finally sent by the server to the browser. At this point all your server-side stuff is disposed.

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

Now the browser can finally show the page with the big "Export to Excel" button which the user can click. Again: the server is already destroying all the data you worked so hard to build. When the user clicks your button, a brand new request is created and a brand new page will be sent back to the browser. You have to load all your data into the grid again.

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

#2


I found the solution to my problem. Thank you all for contributing. On the Load_Page Event I had the binding inside of an if !postback.

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