Click here to Skip to main content
12,302,307 members (23,601 online)
Click here to Skip to main content
Add your own
alternative version

Stats

51.1K views
8 bookmarked
Posted

Export to Excel in ASP.NET C#

, 24 Feb 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Export Gridview data in Excel formate with gridview design.

Introduction

Export Gridview data to Excel formate and provide download that data.

Background 

That provide the your Gridview control data and DataGrid control data into Excel formate with rows and column formate. its also useful to make your own custome design and then export it.

Using the code

Provide very sort and efficient solution of export your data which are display in gridview and datagrid control are exported into excel fomate.

For implementation try to follow below steps..

Step-1   Go your page source and write EnableEventValidation="false" .

             <%@ Page Title="" Language="C#" EnableEventValidation="false"                  MasterPageFile="~/Admin/AdminMaster.master"  AutoEventWireup="true" odeFile="Admin_TotalStudents.aspx.cs" Inherits="Admin_Admin_TotalStudents" %>

 Step-2   Then add below code in button click event.  

  protected void btnexport_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=ExportData1.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.xls";
        StringWriter StringWriter = new System.IO.StringWriter();
        HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);
        
        gridstudentdetails.RenderControl(HtmlTextWriter);
        Response.Write(StringWriter.ToString());
        Response.End();    }  <span style="font-size: 9pt;"> </span>
public override void VerifyRenderingInServerForm(Control control)
    {
        /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
           server control at run time. */
    } 

            

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

JatinKhimani
Software Developer (Junior) THOMSON REUTERS
India India
No Biography provided

You may also be interested in...

Comments and Discussions

 
SuggestionMessage Automatically Removed Pin
17-Jun-14 3:11
memberMember 862432517-Jun-14 3:11 
GeneralMy vote 3 Pin
Cosetter12-Mar-14 0:20
memberCosetter12-Mar-14 0:20 
GeneralRe: My vote 3 Pin
JatinKhimani12-Mar-14 5:21
memberJatinKhimani12-Mar-14 5:21 
GeneralMy vote of 1 Pin
PBGuy24-Feb-14 18:10
professionalPBGuy24-Feb-14 18:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160530.1 | Last Updated 24 Feb 2014
Article Copyright 2014 by JatinKhimani
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid