Tips Station Asp.net Article Seo Articles
Tutorials Code Samples
›  Home
›  Mission
›  About us
›  Contact Us
›  Feedback
›  Terms & Condition
Asp Articles
IT Solutions
 
› ASP.NET

› Programming Tips

› Ajax

› Asp

› ADO.NET

› Databases

› SEO

› CSS And Designing

› Php

 
Most Viewed Articles
 
› Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

› Change theme dynamically without page refresh in ASP.NET

› Install AJAX On Machines Running Visual Studio 2005

› Creating Pretty Popups Using AJAX

› Simple ASP Image Resize Function

› SQL Server Performance Counters

› ASP.Net Interview Questions And Their Answers

› Encode Url using ASP

› Difference Between DataGrid and GridView in Asp.Net

› Select Specific Value WithIn Drop Down List Or Radio Button List

more...
 
 
Home> ASP.NET
 
Generating Ms Excel file from GridView with ASP.NET
 
I was having one requirement of giving an option to the user to generate the Ms Excel sheet from the report that was given to the user in GridView.

Initially it seems difficult but when I worked around, I found really easy way to do that.

First you need to create a .aspx page where GridView will be placed and data will be rendered.

Here is the code for .aspx page

<asp:GridView ID="GridView1" runat="Server" AutoGenerateColumns="True">

        </asp:GridView>

        <br />

        <asp:Button ID="btn" runat="server" OnClick="GenerateExcelFile" Text="Generate Excel File from GridView" />


Now, We have to write code to populate the GridView, in this example i have used one .xml file to populate the data into the GridView.

.xml file as the datasource of the GridView
<?xml version="1.0" encoding="utf-8" ?>

<GridData>

        <Details>

               <FirstName>Sheo</FirstName>

               <LastName>Narayan</LastName>

               <Address>Aurangabad, Bihar</Address>

               <Profession>Job</Profession>

        </Details>

        <Details>

               <FirstName>Vijay</FirstName>

               <LastName>Bandaru</LastName>

               <Address>Hyderabad, AP</Address>

               <Profession>Software Professoinal</Profession>

        </Details>

        <Details>

               <FirstName>Sannat</FirstName>

               <LastName>Digar</LastName>

               <Address>Sanat Nagar, Orrisa</Address>

               <Profession>Job</Profession>

        </Details>

        <Details>

                <FirstName>Suraj</FirstName>

               <LastName>Singh</LastName>

               <Address>Suraj Singh Nagar, Delhi</Address>

               <Profession>Businessman</Profession>

        </Details>

</GridData>


Now, I am going to write the code to populate the GridView in !
IsPostBack of Page_Load event.

DataSet dSet = new DataSet();

        string fileName = Server.MapPath("~/GridData.xml");

        dSet.ReadXml(fileName);

        GridView1.DataSource = dSet.Tables[0].DefaultView;

        GridView1.DataBind();

        dSet.Dispose();


Now, I have my GridView populated with the data.

The main part here is to export this GridView as the Ms Excel file, that will be done in the click event of the [Generate Excel File from GridView] button.

To work with following code you may need to add following code at the top of the .cs page.

using System.IO;
using System.Text;

Here is the code for the button click event
string attachment = "attachment; filename=GridViewExport.xls";

        Response.ClearContent();

        Response.AddHeader("content-disposition", attachment);

        Response.ContentType = "application/ms-excel";

        StringWriter sWriter = new StringWriter();



        HtmlTextWriter htwWriter = new HtmlTextWriter(sWriter);

        GridView1.RenderControl(htwWriter);

        Response.Write(sWriter.ToString());

        Response.End();

Now you have everything ready to test, but wait. When you click the button you will get error "Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.". To avoid this error, Just add the following method into your .cs file.
public override void VerifyRenderingInServerForm(Control control)

    {



    }


Now you have everything into your page, go ahead and click the button. It will ask to open or save the Ms Excel file, do whatever you have to do.

Thats it!
 
 
Vrp Technologies
 
Serversea Hosting
 
 
Latest Articles
 
› Sending SMS With PHP

› MySQL Join Tutorial

› Make An RSS Feed Using PHP

› Intro To Object: Option Variables

› Design An Online Chat Room With PHP And MySQL

› Create Tell A Friend Script With HTML & PHP

› Benchmark And Optimize PHP Script Speed

› What Kind of DBA Are You?

› SQL Server Performance Counters

› SQL Server Performance Tips

more...
 
Random Articles
 
› Clear all Cached objects in Asp.Net

› Writing SEO Friendly URL using HttpHandlers in ASP.NET

› Working with DataSet Objects and XML

› Understanding Transaction in ADO.NET

› Filter Certain Words in a String - Asp.net

› SQL Server Performance Tips

› Storing and Retrieving Variables From Application Object - Asp

› Cache Data within your Application - Asp.Net

› Read and display a text file in Asp.net

› Encode Url using ASP

more...
 
Home Mission About us Contact us Feedback Terms Conditions
2008 © Copyright TipsStation. All rights reserved.