Export to Excel from C # .net

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

<%@ 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 the best explanation for the problem Export from C # .net I have a problem. I have a form when I export to excel. I am using the following <div></div>

I have data in datagrid but not in excel. I just get<div></div>


And this is the function 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 before exporting. This is a line of code. It's funny that I'm creating a new webform with just this section of code and the export worked. Do you think there is a datarepeater issue that I have on the page that is causing the conflict.

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

      

-1


source to share


2 answers


It's not consistency at all - it's just not how asp.net and web work.

You don't populate the grid during the load phase: you fetch the data and set it as the data source for the table, but the grid (actually a relay) is not yet populated. This does not happen until the data binding phase. After the data-binding phase (during the rendering phase), the data relay page is finally sent by the server to the browser. At this point, all of your server stuff is located.



The browser can now finally display a page with a large Export to Excel button that the user can click. Again: the server is already destroying all the data you worked so hard for. When the user clicks the button, a new request is created and the new page will be sent back to the browser. You need to load all data into the grid again.

+1


source


I found a solution to my problem. Thanks everyone for the input. In the Load_Page event, I had a binding inside a back copy of the if.



-1


source







All Articles