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

› Simple ASP Image Resize Function

› Install AJAX On Machines Running Visual Studio 2005

› ASP.Net Interview Questions And Their Answers

› Creating Pretty Popups Using AJAX

› Encode Url using ASP

› SQL Server Performance Counters

› .NET Data Access Performance Comparison

› UpdatePanel control in Asp.net Ajax

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
 
› Session Object in Asp

› Cache Data within your Application - Asp.Net

› How to Start with ASP.NET AJAX

› Search Engine Friendly Web Design - 3 Ways To Use CSS

› CSS Cursors - How To Use Them

› How to Send an Email in Asp.Net

› Filter Certain Words in a String - Asp.net

› Browser Detection in ASP

› ASP.NET Simple Connect To Database

› Your New Website Is Inside

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