To write GridData to Excel File is very simple. This tutorial will show you how to write GridData to an Excel file using ASP.NET 2.0 and C#.

We can use ASP.NET 2.0 to write DataGrid data to Excel file. The method is to write the DataGrid data as stream to Html information, then use FileStream and BinaryWriter to create file and write information to the file.

First, you will need to import the System.IO namespace.

The System.IO namespace contains the StringWriter ,FileStream and BinaryWriter Classes that we need for the sample .

We use the Button1_Click event to do the work.

We use the DataGrid to bind database, then write the DataGrid data as stream to Html information.
After then we use FileStream and BinaryWriter to create file and write information to the file.

The front Default.aspx page looks something like this:

Please add the following code to Web.Config, and change to your User ID and Password accordingly.

Download Source Files