Exporting Data To MS Word in ASP.NET

Previously I have written 4 articles on exporting data from ASP.NET. Chek out all the Export Data Articles I have written.

Download source code here.

Below is the code you can have a look and then I will discuss every bit of it for your understanding.
protected void ExportToWord(object sender, EventArgs e)
    {
        DataTable dt = GetData();
    
        //Create a dummy GridView
        GridView GridView1 = new GridView();
        GridView1.AllowPaging = false;
        GridView1.DataSource = dt;
        GridView1.DataBind();
    
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=MyReport.doc");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-word ";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridView1.RenderControl(hw);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
    }
I guess this must be very clear to everyone what I have written inside. Anyway let's discuss it here.

  1. First, I get the data source from the method GetData(), which is nothing but just a method that returns a datatable with some data inside it. You can have your own dataset here from DB or wherever you want.
  2. Second, I have taken a grid view so that I can render that data in it. If you have a grid view at your page level then no need to take one and bind it here, even you need not to get the data again if you have already that data rendered in any gridview or what in your page.
  3. Third, Add Response headers, content types for Excel download. Remember the Content Type is very important, this is code which decides the file type you are downloading. So here for Excel sheet download the content Type is "application/vnd.ms-word". Also give name to the file you are downloading.
  4. Fourth, Get a string writer and Html writer and render the grid view inside it. Now the string writer will have the exact html of  the grid and we are all set to throw it into an Word File
  5. Fifth, Now write the string writer value into response object and you are all set to download the word file.
If you are looking for the GetData() method to test out the feasibility of this process of exporting data into excel sheet in ASP.NET and C# then here is it.
     private DataTable GetData()
     {
         // Here we create a DataTable with four columns.
         DataTable dtSample = new DataTable();
         dtSample.Columns.Add("Dosage", typeof(int));
         dtSample.Columns.Add("Drug", typeof(string));
         dtSample.Columns.Add("Patient", typeof(string));
         dtSample.Columns.Add("Date", typeof(DateTime));

         // Here we add five DataRows.
         dtSample.Rows.Add(25, "Indocin", "David", DateTime.Now);
         dtSample.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
         dtSample.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
         dtSample.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
         dtSample.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
         dtSample.Rows.Add(25, "Indocin", "David", DateTime.Now);
         dtSample.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
         dtSample.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
         dtSample.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
         dtSample.Rows.Add(25, "Indocin", "David", DateTime.Now);
         dtSample.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
         dtSample.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
         dtSample.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
         dtSample.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
         dtSample.Rows.Add(25, "Indocin", "David", DateTime.Now);
         
         return dtSample;

     }
Hope this article helps you, as this method helped me a lot to overcome the unnecessary usage of external dll for exporting data into excel sheets.

Demo:

Export Data From ASP.NET




2 comments:

  1. Nice article :)
    I do Have some tutorial for mobile development in Xamarin can i upload it too

    ReplyDelete
    Replies
    1. Yes you can, Talk to me when you are available for a discussion.

      Delete