Go to DotNetFunda.com
 Online : 1500 |  Welcome, Guest!   Login
 
Home > Articles > ASP.NET > Simplest Method Export GridviewData to Excelsheet using ASP.Net
  • Nominate yourself for FREE online training by Microsoft MVP on OOPS, ASP.NET, ADO.NET and Sql Server.
    Brought to you by DotNetFunda.Com. You can refer to your friends as well !

  • Now you can recommend your article from any website to be selected as "Article of the Day" on DotNetFunda.Com website. If approved, that article will be featured on our home page.

General Notice: If you found copied contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.

Submit Article | Articles Home | Search Articles |

Simplest Method Export GridviewData to Excelsheet using ASP.Net

 Posted on: 6/23/2009 12:05:09 AM by Syedshakeer | Views: 935 | Category: ASP.NET | Level: Intermediate | Print Article
In this Article you can Know how to Export a Gridview Data to Excel sheet using IO strems.

Ask all your .NET related questions/clarifications here to get quicker solution.

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.





Interesting?   Share and Bookmark this kick it on DotNetKicks.com


Experience:1 year(s)
Home page:http://www.dotnetfunda.com
Member since:Thursday, February 05, 2009
Level:Bronze
Status: [Member]
Biography:Hi to All ..
I am Working as .Net Programmer ....in Hyderabad
 Latest post(s) from Syedshakeer

   ◘ How to get the value of a Hidding column in a Gridview using C# posted on 9/8/2009 6:05:36 PM
   ◘ How to calculate total at the BackEnd using Trigger? posted on 8/25/2009 6:14:14 PM
   ◘ How to display records as First-Next-Previous-Last in a Textboxes using Windows Application? posted on 8/25/2009 6:01:05 PM
   ◘ How to Start Mobile Application on Windows? posted on 8/18/2009 1:00:01 AM
   ◘ Paging for First, Next, Previous and Last in gridview posted on 8/16/2009 12:47:43 PM


Submit Article


About Us | The Team | Advertise | Contact Us | Feedback | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)