SiteMap  | About    

Write content from ASP.NET to Excel

The following program shows how to write the GridView content to an Excel file and save to a desired location.

Here we are using the FileStream class to write the content to a local system.

  Dim fStream As New FileStream("c:\data.xls", FileMode.Create)


  FileStream fStream = new FileStream("c:\\data.xls", FileMode.Create);

The following method will confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.
  Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)

End Sub

  public override void VerifyRenderingInServerForm(Control control)


Somtimes you will get an exception unless you are not declared the above method in your program the exception shows like :


System.Web.HttpException: Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server..



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head id="Head1" runat="server">
<title>Untitled Page</title>
	<form id="form1" runat="server">
	<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" />
	<asp:SqlDataSource ID="SqlDataSource1" runat="server"
	ConnectionString="<%$ ConnectionStrings:SQLDbConnection %>"
	SelectCommand="select * from stores" />
	<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
	Text="Export to Excel" Width="117px" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<asp:Label ID="Label1" runat="server" Text="Message : "></asp:Label>

Click the following links to see full source code

C# Source Code
VB.NET Source Code
default.aspx.vb (C) 2018    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.