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

Task : How convert a gridview to Word Document or in Excel Sheet using asp.net/c#  code.

Description: Some time we need to generate different kind of reports & need to save it in Excel or word Document for Local Reference, Or sending to other's. So here I create a code for achieve this task the first one is conversion data into Excel & second one is for Word Document.

// For pdf converstion we can use third party dll like ITextSharp


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();
    }

14 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
    Replies
    1. Thank You...(But I think you want to post your link there..) :-)

      Delete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
    Replies
    1. I suggest you take my services for asp.net website development(CMS/E-commerce)... :-)

      Delete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. Hello Sir....
    Plz post coadings for HTML to PDF.......

    ReplyDelete
    Replies
    1. you have to work yourself, there is not any ready-made solution....
      search in google you can find many solution there & apply it yourself...

      Delete
  5. This comment has been removed by a blog administrator.

    ReplyDelete
  6. Excellent guidelines for gridview to wordexcel conversion in asp.net.

    ReplyDelete
  7. This comment has been removed by a blog administrator.

    ReplyDelete
  8. This comment has been removed by a blog administrator.

    ReplyDelete
  9. This comment has been removed by a blog administrator.

    ReplyDelete