Go to DotNetFunda.com
 Online : 1296 |  Welcome, Guest!   Login
 
Home > Articles > ASP.NET > Simplest Method Export GridviewData to Excelsheet using ASP.Net

Submit Article | Articles Home | Search Articles |

Simplest Method Export GridviewData to Excelsheet using ASP.Net

red flag  Posted on: 6/23/2009 12:05:09 AM by Syedshakeer | Views: 1526 | Category: ASP.NET | Level: Intermediate


In this Article you can Know how to Export a Gridview Data to Excel sheet using IO strems.



Bind the Data to the Gridview using SqlDatSource and one Button on the webpage.

How to Create a Excel sheet?

TextWriter
is used to Create a Excel Sheet by creting the StreamWriter class object.StreamWriter
class takes the path the of the excel sheet name as argument.Dont forget to use
the verbatile string (@) at the beginning of the path as follows:


TextWriter sw = new StreamWriter(@"F:\\ExcelData.xls");

 

The Extension of the Excel Sheet is .xls.It will automatically creats the excel
sheet with the name ExcelData in ‘F’Drive.If the Excel sheet is already
existing with the name ‘ExcelData’ it will overrides the Data.

in .asp.cs page


using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

using System.IO;

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

 

//Double click your Button and write the below code to Export Gridview Data to Excel

    protected void Button1_Click(object sender, EventArgs e)

    {

    TextWriter sw = new StreamWriter(@"F:\\ExcelData.xls");

    //Getting the Gridview HeaderRow Values

sw.WriteLine(GridView1.HeaderRow.Cells[0].Text+"\t"+GridView1.HeaderRow.Cells[1].Text+"\t"

        +GridView1.HeaderRow.Cells[2].Text);

 

        for (int i = 0; i < GridView1.Rows.Count; i++)

        {

            //Getting the Gridview Row values

            sw.WriteLine(GridView1.Rows[i].Cells[0].Text  + "\t " + GridView1.Rows[i].Cells[1].Text  + "\t" + GridView1.Rows[i].Cells[2].Text );

        }

        sw.Close();//Don’t Forget to Close the StreamWrtier object.

    }

}


output of Excel sheet with data.



The Following Error occurs when you try to export a Data to Excel when the Excel Sheet is in open State.

Error Iamge.





If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Found interesting? Add this to:

| More



Please Sign In to vote for this post.

 
Latest post(s) from Syedshakeer

Latest Articles
Experience:2 year(s)
Home page:http://www.dotnetfunda.com
Member since:Thursday, February 05, 2009
Level:Starter
Status: [Member]
Biography:Shakeer Hussain has completed his Master of Computer Applications degree from Deccan College of engg and technology of Osmania University.He is a MVM of www.dotnetspider.com.He has good experience in the areas of ASP.NET, C#.NET, VB.NET, SQL SERVER 2000/2005 and Windows Mobile. He has worked in Windows Mobile,Web Applicatin and ERP projects.

Submit Article

About Us | The Team | Advertise | Contact Us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 9/3/2010 3:48:59 AM