Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
Tutorial RSS
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Charts
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - File - To write GridData to Excel File using ASP.NET 2.0 (C#)
To write GridData to Excel File using ASP.NET 2.0 (C#)


ASP.NET File Tutorial

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 .

Imports System.IO

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

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.

protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "")
{
Response.Write("<script language="javascript">window.alert('Please enter filename!');</script>");
return;
}
else
{
string filename = TextBox1.Text;

this.DataGrid1.Page.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
this.DataGrid1.RenderControl(hw);
string HtmlInfo = tw.ToString().Trim();

string DocFileName = filename+".xls";
string FilePathName = Request.PhysicalPath;
FilePathName = FilePathName.Substring(0, FilePathName.LastIndexOf("\\"));

FilePathName = FilePathName + "\\" + DocFileName;
File.Delete(FilePathName);
FileStream Fs = new FileStream(FilePathName, FileMode.Create);
BinaryWriter BWriter = new BinaryWriter(Fs, Encoding.GetEncoding("UTF-8"));

BWriter.Write(HtmlInfo);
BWriter.Close();
Fs.Close();
}
}

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

The front Default.aspx page looks something like this:

<body>
<form id="Form1" method="post" runat="server">
<fieldset>
<legend>DataToExcel</legend>
<asp:Button id="Button1" runat="server"
Text="ToExcel" OnClick="Button1_Click"></asp:Button>
&nbsp; Save as filename:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:datagrid id="DataGrid1" runat="server" width="100%" DataSourceID="SqlDataSource1">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<FooterStyle HorizontalAlign="Center"></FooterStyle>
<PagerStyle PageButtonCount="15" Mode="NumericPages"></PagerStyle>
</asp:datagrid><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [EmployeeID], [LastName], [FirstName], [BirthDate], [City], [Address], [Country], [HomePhone] FROM [Employees]">
</asp:SqlDataSource>
</fieldset>
</form>
</body>

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

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

<connectionStrings>
<add name="NorthwindConnectionString" connectionString="Data Source=localhost;Initial Catalog=Northwind;User ID=sa" providerName="System.Data.SqlClient" />
</connectionStrings>

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!



 
  Developer Resources







Server Intellect Rocks