How Convert GridView to Word or Excel Asp.Net using C sharp Code

Category > ASP.NET || Published on : Monday, September 1, 2014 || Views: 13743 || GridView to Word or Excel Asp.Net using C sharp Code gridview to word export gridview to word or excel GridView with word or excel


Introuction:-

In this tutorial we will learn How convert a gridview to Word Document or in Excel Sheet using asp.net/c#  code.

Description:-

Generally we need to generate different kind of reports & need to save it in MS-Excel or Ms-word Document for future Reference, Or sending emails or sharing with others. So to acheive here I create a code for achieve this task the first one is conversion data into Excel & second one is for Word Document.

so lets starts the coding.

Step 1: First, we start by creating an Empty ASP.NET web site in Visual Studio .NET 2010. The AJAX Extensions (from Microsoft) make it a whole lot easier to create AJAX web pages


http://www.sourcecodehub.com/articleimages/ajax-timer-control-in-aspnet/Create-website-visual-studio-1

Create-website-visual-studio-new-website2


Step 2: Create an ASPX Page for Right Click on Solution Explorer > Add Items > Web > Webform and save it as "Default.aspx".When we first open our Default.aspx page

add-new-webform-visual-studio-3

add-new-webform-visual-studio-add-name4

 

Step 3: Add a GridView, button on that particular ASP.NET Webform. After that bind GridView with any data source or you can write codes for that also.

Step 4: Add the following codes to the button click event on the webpage


C# Code : For GridView To Excel

 protected void btn_excel_Click(object sender, EventArgs e)
    {
          string attachment = "attachment; filename=myreport.xls";
          Response.Cache.SetCacheability(HttpCacheability.NoCache);
          Response.AddHeader("content-disposition", attachment);
          Response.ContentType = "application/ms-excel";
          StringWriter swriter = new StringWriter();
          HtmlTextWriter htmlwriter = new HtmlTextWriter(swriter);
 
          // Create a form to contain the gridview(MyGridView)
          HtmlForm mynewform = new HtmlForm();
          MyGridView.Parent.Controls.Add(mynewform);
          mynewform.Attributes["runat"] = "server";
          mynewform.Controls.Add(MyGridView);
          mynewform.RenderControl(htmlwriter);
          Response.Write(swriter.ToString());
          Response.End();
    }


C# Code : For GridView To Word

    protected void btn_word_Click(object sender, EventArgs e)
    {
          Response.AddHeader("content-disposition", "attachment;filename=myreport.doc");
          Response.Cache.SetCacheability(HttpCacheability.NoCache);
          Response.ContentType = "application/vnd.word";
          System.IO.StringWriter swriter = new System.IO.StringWriter();
          System.Web.UI.HtmlTextWriter htmlwriter = new HtmlTextWriter(swriter);
 
          // Create a form to contain the gridview(MyGridView)
          HtmlForm mynewform = new HtmlForm();
          MyGridView.Parent.Controls.Add(mynewform);
          mynewform.Attributes["runat"] = "server";
          mynewform.Controls.Add(MyGridView);
          mynewform.RenderControl(htmlwriter);
          Response.Write(swriter.ToString());
          Response.End();
    }

Run the applcation by pressing F5, this will open the webpage having the Griview populated with data.
Now, Press the button to export Gridview data into Excel or Word.

Conclusion, Here we have learned, How Convert GridView to Word or Excel Asp.Net using C# Code

Download Source Codes