

Navigator : Home > Tutorials > File Tutorials > ...
To write GridData to Excel File using ASP.NET 2.0 (C#)
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.
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!
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.
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(); } } |
The front Default.aspx page looks something like this:
I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.
<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> 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> |
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> |
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!
Looking for the VB.NET 2005 Version? Click Here!