Skip to main content

Export Grid View of .Net to Excel


Dear All,

Here is a new article for exporting your Grid View to Excel. Feel Free to ask your queries. Following are the steps for this:

1. Create a Grid View on Web Page.
2. Bind grid view with data.  For binding of gridview you can refer to the previous article "Dynamic Gridview Bind" Url: http://dinesh-itsolutions.blogspot.in/2010/09/dynamic-gridview-bind.html.

3. After binding the grid view put a button on the web page named "Export to Excel".  On Clicking of that button you can copy and paste the following code.

protected void btnExport(object sender, EventArgs e)
{
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition",                                            "attachment;filename=ExportedFile.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";
    using (StringWriter sw = new StringWriter())
    {
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        //To Export all pages
        GridView1.AllowPaging = false;
        this.gridbind();
        GridView1.HeaderRow.BackColor = Color.White;
        foreach (TableCell cell in GridView1.HeaderRow.Cells)
        {
            cell.BackColor = GridView1.HeaderStyle.BackColor;
        }
        foreach (GridViewRow row in GridView1.Rows)
        {
            row.BackColor = Color.White;
            foreach (TableCell cell in row.Cells)
            {
                if (row.RowIndex % 2 == 0)
                {
                    cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
                }
                else
                {
                    cell.BackColor = GridView1.RowStyle.BackColor;
                }
                cell.CssClass = "textmode";
            }
        }
        GridView1.RenderControl(hw);
           
        string style = @"<style> .textmode { } </style>";
        Response.Write(style);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
    }
}
  //This is necessary event.So do copy/paste this also.
public override void VerifyRenderingInServerForm(Control control)
{
 
}

On Clicking of the button. You can successfully export your grid view to the excel.
Hope you like the article. :-)
 

Comments

Popular posts from this blog

The Page Life Cycle of an ASP.NET and its controls.

The life cycle starts when a user requests a web page through his/her browser. The Web server than process the page through a sequence of steps before response is sent back to the user's browser. The steps are as: Page Request Start Page Initialization Load Validation PostBack Event Handling Render Unload Page Request The page request occurs before the page life cycle begins. When a user requests the page, ASP.NET determines whether the page needs to be parsed and compiled (therefore beginning the life of a page), or whether a cached version of the page can be sent in response without running the page. Start In the start step, page properties such as Request and Response are set. At this stage, the page also determines whether the request is a postback or a new request and sets the IsPostBack property. Additionally, during the start step, the page's UICulture property is set. Page Initialization During page initialization, controls on ...

Embed Tiny Editor in Your Web Application:

1 . First of all open your Web page in your application in which you want to embed the Editor. 2 . This editor is know as Tiny Editor which has nice features by which the user can enter his/her text in stylish way by embedding smileyes, embed photos. 3 . We can set font family, Font Size, Cut, Copy, Paste and many other functions are there by which the text can be represented in a better way. 4 . Now Steps to embed the Tiny Editor in you web page are: a : Drag and Drop Textbox and give id to id name RichTextBox. b : Set the Property of Textbox:-TextMode Property to Multiline. c : Drop one Label to page, we transfer the text from RichText Box to Label when we store the text to database. d : Put this Code in your Head Tag:   < script type ="text/javascript" src ="tinymce/jscripts/tiny_mce/tiny_mce.js"></ script > < script type ="text/javascript">    tinyMCE.init(     {         mode : "textareas...

Introduction to .NET and .NET Architecture

What is .NET? Microsoft .NET (pronounced “dot net”) is a software component that runs on the Windows operating system. .NET provides tools and libraries that enable developers to create Windows software much faster and easier. .NET benefits end-users by providing applications of higher capability, quality and security. The .NET Framework must be installed on a user’s PC to run .NET applications. This is how Microsoft describes it: “.NET is the Microsoft Web services strategy to connect information, people, systems, and devices through software. Integrated across the Microsoft platform, .NET technology provides the ability to quickly build, deploy, manage, and use connected, security-enhanced solutions with Web services.     What is the .NET architecture? Microsoft .NET consists of four major components: Common Language Specification (CLS) – blue in the diagram below Framework Class Library (FCL) – red Common Language Runtime (CLR) – gr...