ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Export gridview with controls to excel sheet in ASP.Net<p>Hi mehad,</p> <p>I have created sample using your code.</p> <p><span style="text-decoration: underline;">HTML</span></p> <pre class="brush: html">&lt;div&gt; &lt;asp:Button ID=&#34;btnExport_Page&#34; CssClass=&#34;add&#34; runat=&#34;server&#34; OnClick=&#34;btnExport_Page_Click&#34; Text=&#34;export page&#34;&gt;&lt;/asp:Button&gt; &lt;/div&gt; &lt;asp:GridView runat=&#34;server&#34; AutoGenerateColumns=&#34;False&#34; DataKeyNames=&#34;CustomerId&#34; AllowPaging=&#34;true&#34; PageSize=&#34;5&#34; ID=&#34;gvProcessReport&#34; DataSourceID=&#34;SqlDataSource1&#34; OnPageIndexChanging = &#34;OnPaging&#34;&gt; &lt;Columns&gt; &lt;asp:TemplateField HeaderText=&#34;CustomerId&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label1&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;CustomerId&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Name&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:TextBox ID=&#34;txtMessage&#34; TextMode=&#34;MultiLine&#34; runat=&#34;server&#34; Enabled=&#34;false&#34; Text=&#39;&lt;%# Eval(&#34;Name&#34;) %&gt;&#39; Width=&#34;97%&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;ItemStyle Width=&#34;200px&#34; /&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Country&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label3&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Country&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;asp:SqlDataSource ID=&#34;SqlDataSource1&#34; runat=&#34;server&#34; ConnectionString=&#34;&lt;%$ ConnectionStrings:TestConnectionString %&gt;&#34; SelectCommand=&#34;SELECT * FROM [Customers]&#34;&gt;&lt;/asp:SqlDataSource&gt;</pre> <p><span style="text-decoration: underline;">Code</span></p> <pre class="brush: csharp">public void ExportOnlyOnePage() { Response.Clear(); Response.Buffer = true; Response.AddHeader(&#34;content-disposition&#34;, &#34;attachment;filename=GridViewExport.xls&#34;); Response.Charset = &#34;&#34;; Response.ContentType = &#34;application/vnd.ms-excel&#34;; using (StringWriter sw = new StringWriter()) { HtmlTextWriter hw = new HtmlTextWriter(sw); gvProcessReport.AllowPaging = false; gvProcessReport.DataBind(); gvProcessReport.HeaderRow.BackColor = System.Drawing.Color.White; foreach (TableCell cell in gvProcessReport.HeaderRow.Cells) { cell.BackColor = gvProcessReport.HeaderStyle.BackColor; } foreach (GridViewRow row in gvProcessReport.Rows) { row.BackColor = System.Drawing.Color.White; foreach (TableCell cell in row.Cells) { if (row.RowIndex % 2 == 0) { cell.BackColor = gvProcessReport.AlternatingRowStyle.BackColor; } else { cell.BackColor = gvProcessReport.RowStyle.BackColor; } cell.CssClass = &#34;textmode&#34;; } } gvProcessReport.RenderControl(hw); string style = @&#34;&lt;style&gt; .textmode { } &lt;/style&gt;&#34;; Response.Write(style); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); } } public override void VerifyRenderingInServerForm(Control control) { /* Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time. */ } protected void OnPaging(object sender, GridViewPageEventArgs e) { GridView gv = (GridView)gvProcessReport; gv.PageIndex = e.NewPageIndex; gv.DataBind(); } protected void btnExport_Page_Click(object sender, EventArgs e) { ExportOnlyOnePage(); }</pre> <p><span style="text-decoration: underline;">Screenshot</span></p> <p><img src="https://i.imgur.com/9drinYa.gif" alt="" width="472" height="409" /></p>https://www.aspforums.net:443/Threads/138949/Export-gridview-with-controls-to-excel-sheet-in-ASPNet/https://www.aspforums.net:443/Threads/138949/Export-gridview-with-controls-to-excel-sheet-in-ASPNet/Thu, 09 Jun 2016 06:59:29 GMT