title: Exporting data from c# to word document
body: <p>I have a grid in c# filled with data. One of the columns in that grid contains letters (followed by numbers) sorted alphabetically, like this:</p>
  
  <pre><code>A124
  A256
  A756
  B463
  B978
  D322
  etc.
  </code></pre>
  
  <p>I need to export this data in a word document (.doc or .docx format).
  This is what i did to export a signle grid:</p>
  
  <pre><code>var dt = FuntionThatReturnsDatatables();
  
  var gd = new GridView
          {
              DataSource = dt                     
          };
  
          gd.DataBind();
  
          HttpContext.Current.Response.Clear();
          HttpContext.Current.Response.Buffer = true;
  
          HttpContext.Current.Response.AddHeader("content-disposition", "attachment;
          filename=" + "List" + DateTime.Now.ToString("dd.MM.yyyy") + ".doc");
  
          HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
          HttpContext.Current.Response.ContentType = "application/ms-word";
          HttpContext.Current.Response.Charset = "UTF-8";
  
        HttpContext.Current.Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble()); 
  
          var oStringWriter = new StringWriter();
          var oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
  
          gd.RenderControl(oHtmlTextWriter);
          HttpContext.Current.Response.Output.Write(oStringWriter.ToString());
          HttpContext.Current.Response.Flush();
          HttpContext.Current.Response.End();
  </code></pre>
  
  <p>But now I have to follow this logic:
   - For every letter from grid a new table with title should be created like this:</p>
  
  <pre><code>Table A:
   A124
   A256
   A756
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/cnUZ7.png" alt="enter image description here"></p>
  
  <p><img src="https://i.stack.imgur.com/hkqig.png" alt="enter image description here"></p>
  
  <ul>
  <li><p>Each new table should start from a new page, like this:</p>
  
  <p>Table A:
      A124,
      A256,
      A756,
      //new page</p>
  
  <p>Table B:
  B463,
  B978,
  //new page</p>
  
  <p>Table D:
  D322,
  etc.</p></li>
  <li><p>Pages in that word document need to be numbered.</p></li>
  </ul>
  
  <p>Is there any way to write a code in c# to do this or is there some library/plugin that can accomplish this task ? </p>
  
  <p>Some examples would be appreciated.</p>