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

Category > ASP.NET || Published on : Thursday, October 2, 2014 || Views: 13205 || GridView to Word GridView to Excel


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: Create a new ASP.NET empty Website using Visual Studio 2010. You can download the Visual Studio here


Step 2: Create a new ASP.NET Web page by right clicking on the Solution Explorer or from the File Menu and made the neccessary changes as below.

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 Codes below for this article( How convert a gridview to Word Document or in Excel Sheet using asp.net/c#  code)

 

 

 

Download Source Codes