title: How to export nested gridview to excel/word with gridlines on the child grid
body: <p>I have tried posting questions on different forum, but there are still no response.. can anyone help me really? i have tried most codings i gathered on exporting nested gridview.. however most of them gave me the same output.. in fact what i want was quite "simple"</p>
  
  <p>code(excel)</p>
  
  <pre><code>protected void Export(GridView gridView)
  {
      HttpContext.Current.Response.Clear();
      HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "StaffAppraisal.xls"));
      HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
  
      //  Create a table to contain the grid
      Table table = new Table();
  
      //  include the gridline settings
      table.GridLines = gridView.GridLines;
  
      //  add the header row to the table
      if (gridView.HeaderRow != null)
      {
          PrepareControlForExport(gridView.HeaderRow);
          gridView.HeaderRow.Style.Add("background-color", "");
          table.Rows.Add(gridView.HeaderRow);
      }
  
      //  add each of the data rows to the table
  
      foreach (GridViewRow row in gridView.Rows)
      {
          PrepareControlForExport(row);
          table.Rows.Add(row);
      }
  
      //  add the footer row to the table
      if (gridView.FooterRow != null)
      {
          PrepareControlForExport(gridView.FooterRow);
          table.Rows.Add(gridView.FooterRow);
      }
  
      for (int i = 0; i &lt;= gridView.Rows.Count; i++)
      {
          table.Rows[i].Cells[0].Visible = false;
      }
  
      using (StringWriter stringWriter = new StringWriter())
      {
          using (HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter))
          {
              //  render the table into the htmlwriter
              table.RenderControl(htmlWriter);
  
              //  render the htmlwriter into the response
              HttpContext.Current.Response.Write(stringWriter.ToString());
              HttpContext.Current.Response.End();
          }
      }
  }
  </code></pre>
  
  <p>code(word)</p>
  
  <pre><code>public static void Export(GridView gridView)
  {
      HttpContext.Current.Response.Clear();
      HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "StaffAppraisal.doc"));
      HttpContext.Current.Response.ContentType = "application/ms-word";
      //  Create a table to contain the grid
      Table table = new Table();
  
      //  include the gridline settings
      table.GridLines = gridView.GridLines;
  
      //  add the header row to the table
      if (gridView.HeaderRow != null)
      {
          PrepareControlForExport(gridView.HeaderRow);
          table.Rows.Add(gridView.HeaderRow);
      }
  
      for (int j = 0; j &lt; gridView.Columns.Count; j++)
      {
          //Apply style to Individual Cells
          gridView.HeaderRow.Cells[j].Style.Add("background-color", "black");
      }
  
      //  add each of the data rows to the table
      foreach (GridViewRow row in gridView.Rows)
      {
          PrepareControlForExport(row);
          table.Rows.Add(row);
      }
  
      //  add the footer row to the table
      if (gridView.FooterRow != null)
      {
          PrepareControlForExport(gridView.FooterRow);
          table.Rows.Add(gridView.FooterRow);
      }
  
      for (int i = 0; i &lt;= gridView.Rows.Count; i++)
      {
          table.Rows[i].Cells[0].Visible = false;
      }
  
      using (StringWriter stringWriter = new StringWriter())
      {
          using (HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter))
          {
              //  render the table into the htmlwriter
              table.RenderControl(htmlWriter);
  
              //  render the htmlwriter into the response
              HttpContext.Current.Response.Write(stringWriter.ToString());
              HttpContext.Current.Response.End();
          }
      }
  }
  </code></pre>
  
  <p>the image below is exported from excel...</p>
  
  <p><img src="https://i.stack.imgur.com/2janI.png" alt="exported to excel"></p>
  
  <p>the image below is exported from word...</p>
  
  <p><img src="https://i.stack.imgur.com/KaAoR.png" alt="exported to word"></p>