title: Export to Excel from c# .net
body: <p>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.</p>
  
  <pre><code>&lt;%@ 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" %&gt;
  &lt;%@ Register TagPrefix="UC" TagName="Paging" Src="~/controls/Paging.ascx" %&gt;
  &lt;asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"&gt;
  
      &lt;script type="text/javascript" src="sorttable.js"&gt;&lt;/script&gt;
   Go to &lt;a href="members-search.aspx" class="link"&gt;Quick Search&lt;/a&gt;
  &lt;h1&gt;Contact Database - Advanced Search&lt;/h1&gt;
  &lt;asp:Label ID="lblSearchCriteria" runat="server" Text=""  ForeColor="blue"&gt;&lt;/asp:Label&gt;
  &lt;table width="100%" cellspacing="0" cellpadding="0"&gt;
      &lt;tr&gt;
          &lt;td valign="top" width="50%"&gt;
  &lt;table border="0" cellpadding="3" cellspacing="1" style="height: 214px; width: 101%;"&gt;  
      &lt;tr&gt;
          &lt;td class="form1" align="right"&gt;&lt;strong&gt;OpenSearch:&lt;/strong&gt;&lt;/td&gt;
          &lt;td class="form2"&gt;
              &lt;asp:TextBox ID="txtSearch" runat="server" CssClass="fields" Width="245px" &gt;&lt;/asp:TextBox&gt;&lt;b&gt;*&lt;/b&gt;&amp;nbsp;&lt;br&gt;&lt;b&gt;*&lt;/b&gt;&lt;small&gt;Search By Company Name, First Name, Last Name, Tags, Comments&lt;/small&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td class="form1" align="right"&gt;&lt;strong&gt;Industry Segment:&lt;/strong&gt;&lt;/td&gt;
          &lt;td class="form2"&gt;&lt;asp:DropDownList ID="ddlIndSegment" runat="server" CssClass="fields" /&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td class="form1" align="right"&gt;&lt;strong&gt;Member Type:&lt;/strong&gt;&lt;/td&gt;
          &lt;td class="form2"&gt;&lt;asp:DropDownList ID="ddlMemberType" runat="server" CssClass="fields" /&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td class="form1" align="right"&gt;&lt;strong&gt;Member Rep only:&lt;/strong&gt;&lt;/td&gt;
          &lt;td class="form2"&gt;&lt;asp:CheckBox ID="cbxMemberRep" runat="server" /&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td class="form1" align="right"&gt;&lt;strong&gt;Board Only:&lt;/strong&gt;&lt;/td&gt;
          &lt;td class="form2"&gt;&lt;asp:CheckBox ID="cbxBoardOnly" runat="server" /&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td colspan="2" align="right"&gt;
              &lt;asp:ImageButton ID="btnSearch" runat="server" ImageUrl="/RadControls/images/bu_search.jpg" /&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;/table&gt;
      &lt;/td&gt;
          &lt;td&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/td&gt;
      &lt;td valign="top" width="50%"&gt;
      &lt;%--split the table here--%&gt;
          &lt;table border="0" cellpadding="3" cellspacing="1" 
              style="width: 98%; margin-left: 0px;"&gt;
      &lt;tr&gt;
          &lt;td class="form1" align="right"&gt;&lt;strong&gt;Suspended Only:&lt;/strong&gt;&lt;/td&gt;
          &lt;td class="form2"&gt;&lt;asp:CheckBox ID="cbxSuspended" runat="server" /&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td class="form1" align="right"&gt;&lt;strong&gt;Active Only:&lt;/strong&gt;&lt;/td&gt;
          &lt;td class="form2"&gt;&lt;asp:CheckBox ID="cbxActiveOnly" runat="server" /&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td class="form1" align="right"&gt;&lt;strong&gt;Allow Other Members To See My Info Only:&lt;/strong&gt;&lt;/td&gt;
          &lt;td class="form2"&gt;&lt;asp:CheckBox ID="cbxAllowOtherMembers" runat="server" /&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td class="form1" align="right"&gt;&lt;strong&gt;State:&lt;/strong&gt;&lt;/td&gt;
          &lt;td class="form2"&gt;&lt;asp:DropDownList ID="ddlState" runat="server" CssClass="fields" /&gt;&lt;/td&gt;
      &lt;/tr&gt;
          &lt;tr&gt;
          &lt;td class="form1" align="right"&gt;&lt;strong&gt;By Special Level:&lt;/strong&gt;&lt;/td&gt;
          &lt;td class="form2"&gt;&lt;asp:DropDownList ID="ddlSpecialLevel" runat="server" CssClass="fields" /&gt;&lt;/td&gt;
      &lt;/tr&gt;
  
      &lt;tr&gt;
          &lt;td class="form1" align="right"&gt;&lt;strong&gt;Sort by:&lt;/strong&gt;&lt;/td&gt;
          &lt;td class="form2"&gt;
              &lt;asp:DropDownList ID="ddlSortBy" runat="server" CssClass="fields"&gt;
                  &lt;asp:ListItem Value="CompanyName"&gt;Company&lt;/asp:ListItem&gt;    
                  &lt;asp:ListItem Value="FirstName"&gt;First Name&lt;/asp:ListItem&gt;
                  &lt;asp:ListItem Value="LastName"&gt;Last Name&lt;/asp:ListItem&gt;
                  &lt;asp:ListItem Value="MemberCategoryId"&gt;Membership Type&lt;/asp:ListItem&gt;
                  &lt;asp:ListItem Value="IndustrySegmentId"&gt;Industry Segment&lt;/asp:ListItem&gt;
              &lt;/asp:DropDownList&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  
  &lt;/table&gt;    
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
      &lt;table  border="0" cellpadding="3" cellspacing="1" width="100%"&gt;        
  &lt;tr&gt;
  &lt;td&gt;&lt;asp:Literal ID="litTotalCount" runat="server" /&gt;&lt;/td&gt;&lt;td align=left class="form3"&gt;
      &lt;asp:Button ID="Button1" runat="server"  onclick="Button1_Click" Text="Button" /&gt;
      &lt;asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click"&gt;Export To Excel&lt;/asp:LinkButton&gt;
      &lt;small&gt;* Click on the column to SORT&lt;/small&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;
  
      &lt;asp:Repeater ID="rptList" runat="server"&gt;
          &lt;HeaderTemplate&gt;
          &lt;table class="sortable" border="0" cellpadding="3" cellspacing="1" width="100%"&gt;        
              &lt;tr&gt;
                  &lt;td align="left" valign="top" class="form1"&gt;&lt;b&gt;Company&lt;/b&gt;&lt;/td&gt;
                  &lt;td align="left" valign="top" class="form1"&gt;&lt;b&gt;First Name&lt;/b&gt;&lt;/td&gt;
                  &lt;td align="left" valign="top" class="form1"&gt;&lt;b&gt;Last Name&lt;/b&gt;&lt;/td&gt;
                  &lt;td align="left" valign="top" class="form1"&gt;&lt;b&gt;Email&lt;/b&gt;&lt;/td&gt;
              &lt;/tr&gt;
          &lt;/HeaderTemplate&gt;
          &lt;ItemTemplate&gt;
              &lt;tr&gt;
                  &lt;td class="form2" align="left" valign="top"&gt;&lt;asp:HyperLink ID="hlCompany" runat="server" CssClass="link"&gt;&lt;/asp:HyperLink&gt;&lt;/td&gt;
                  &lt;td class="form2" align="left" valign="top"&gt;&lt;asp:HyperLink ID="hlFirstName" runat="server" CssClass="link"&gt;&lt;/asp:HyperLink&gt;&lt;/td&gt;
                  &lt;td class="form2" align="left" valign="top"&gt;&lt;asp:HyperLink ID="hlLastName" runat="server" CssClass="link"&gt;&lt;/asp:HyperLink&gt;&lt;/td&gt;
                  &lt;td class="form2" align="left" valign="top"&gt;&lt;asp:HyperLink ID="hlEmail" runat="server" CssClass="link"&gt;&lt;/asp:HyperLink&gt; &lt;/td&gt;
              &lt;/tr&gt;
          &lt;/ItemTemplate&gt;
          &lt;FooterTemplate&gt;
          &lt;/table&gt;
          &lt;/FooterTemplate&gt;
      &lt;/asp:Repeater&gt;
  
      &lt;asp:GridView ID="GridView2" runat="server"&gt;
      &lt;/asp:GridView&gt;
  
      &lt;asp:PlaceHolder ID="PlaceHolder1" runat="server"&gt;&lt;br /&gt;
      &lt;UC:Paging Id="ctlPaging" runat="server" /&gt;
      &lt;/asp:PlaceHolder&gt;
  &lt;%--    &lt;div align="center"&gt;
          &lt;br /&gt;&lt;UC:Paging Id="ctlPaging" runat="server" /&gt;
      &lt;/div&gt;--%&gt;
  
  &lt;/asp:Content&gt;
  </code></pre>
  
  <hr>
  
  <p>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 <code>&lt;div&gt;&lt;/div&gt;</code> I have data in the datagrid but not in excel I am just getting <code>&lt;div&gt;&lt;/div&gt;</code></p>
  
  <hr>
  
  <p>And this is the function that I am using </p>
  
  <pre><code>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)
   {
  
   }
  </code></pre>
  
  <hr>
  
  <p>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.</p>
  
  <pre><code>MemberList list = MemberDB.GetMembers(sql, m_page, m_RecordPerPage, out count, _state);
            this.GridView2.DataSource = list;
            this.GridView2.DataBind();
  </code></pre>