How to 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.
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 override void VerifyRenderingInServerForm(Control control)
{
}
Sometimes 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..
Default.aspx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SQLDbConnection %>"
SelectCommand="select * from stores" />
</div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Export to Excel" Width="117px" />
<asp:Label ID="Label1" runat="server" Text="Message : "></asp:Label>
</form>
</body>
</html>
Best ASP.NET Hosting Recommendation
ASPHostPortal.com provides its customers with Plesk Panel, one of the most popular and stable control panels for Windows hosting, as free. You could also see the latest .NET framework, a crazy amount of functionality as well as Large disk space, bandwidth, MSSQL databases and more. All those give people the convenience to build up a powerful site in Windows server. ASPHostPortal.com offers ASP.NET hosting starts from $1/month only. They also guarantees 30 days money back and guarantee 99.9% uptime. If you need a reliable affordable ASP.NET Hosting, ASPHostPortal.com should be your best choice.