title: How to export the entire datagridview into a word document (using office word interop DLL in C#)
body: <p>How to update/write/save/export the entire DataTable or datagridview into a same word document(Several times) using office word interop DLL?iam able to achieve it but the thing is i cant save DataTable into a single document , evertime it saves as new document wont save in a previous document. 
      `using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq; 
      using System.Text;
      using System.Windows.Forms;
      using System.IO;
      using Word = Microsoft.Office.Interop.Word;
      using System.Reflection;</p>
  
  <pre><code>namespace Save_DataTable
  {
  public partial class Form1 : Form
  {
      public Form1()
      {
          InitializeComponent();
      }
  
      public string data_path;
      public string File_path;
      public string filename;
  
      private void Form1_Load(object sender, EventArgs e)
      {
          DataTable dt = new DataTable();
          dt.Columns.AddRange(new DataColumn[3] { new DataColumn("x",            
     typeof(int)), new DataColumn("y", typeof(string)), new   
     DataColumn("z",  
     typeof(string)) });
          dt.Rows.Add(1, "1", "1");
          dt.Rows.Add(2, "2", "2");
          dt.Rows.Add(3, "3", "3");
          dt.Rows.Add(4, "4", "4");
          this.dataGridView1.DataSource = dt;
      }
  
      private void Save()
      {
          if (dataGridView1.Rows.Count != 0)
          {
              int RowCount = dataGridView1.Rows.Count;
              int ColumnCount = dataGridView1.Columns.Count;
              Object[,] DataArray = new object[RowCount + 1, ColumnCount 
         + 
          1];
  
              //add rows
              int r = 0;
              for (int c = 0; c &lt;= ColumnCount - 1; c++)
              {
                  for (r = 0; r &lt;= RowCount - 1; r++)
                  {
                      DataArray[r, c] = 
         dataGridView1.Rows[r].Cells[c].Value;
                  } //end row loop
              } //end column loop
  
              Word.Document oDoc = new Word.Document();
              //oDoc.Application.Visible = false;
  
              Word.Application app = new Word.Application();
  
              app.Application.Visible = false;
  
              try
              {
                  oDoc = app.Documents.Open(filename);
  
              }
              catch
              {
              }
              //page orintation
              oDoc.PageSetup.Orientation = 
            Word.WdOrientation.wdOrientLandscape;
  
  
              dynamic oRange = oDoc.Content.Application.Selection.Range;
              string oTemp = "";
              for (r = 0; r &lt;= RowCount - 1; r++)
              {
                  for (int c = 0; c &lt;= ColumnCount - 1; c++)
                  {
                      oTemp = oTemp + DataArray[r, c] + "\t";
  
                  }
              }
  
              //table format
              oRange.Text = oTemp;
  
              object Separator =  
           Word.WdTableFieldSeparator.wdSeparateByTabs;
              object ApplyBorders = true;
              object AutoFit = true;
              object AutoFitBehavior = 
           Word.WdAutoFitBehavior.wdAutoFitContent;
  
              oRange.ConvertToTable(ref Separator, ref RowCount, ref 
              ColumnCount,
                                    Type.Missing, Type.Missing, ref 
               ApplyBorders,
                                    Type.Missing, Type.Missing, 
               Type.Missing,
                                    Type.Missing, Type.Missing, 
                Type.Missing,
                                    Type.Missing, ref AutoFit, ref 
                AutoFitBehavior, Type.Missing);
  
              oRange.Select();
  
              oDoc.Application.Selection.Tables[1].Select();
  
  
          oDoc.Application.Selection.Tables[1].Rows.AllowBreakAcrossPages 
          = 0;
              oDoc.Application.Selection.Tables[1].Rows.Alignment = 0;
              oDoc.Application.Selection.Tables[1].Rows[1].Select();
              oDoc.Application.Selection.InsertRowsAbove(1);
              oDoc.Application.Selection.Tables[1].Rows[1].Select();
  
              //header row style
              oDoc.Application.Selection.Tables[1].Rows[1].Range.Bold = 
              1;
  
             oDoc.Application.Selection.Tables[1].Rows[1].Range.Font.Name 
             = "Tahoma";
  
            oDoc.Application.Selection.Tables[1].Rows[1].Range.Font.Size 
           = 14;
  
              //add header row manually
              for (int c = 0; c &lt;= ColumnCount - 1; c++)
              {
                  oDoc.Application.Selection.Tables[1].Cell(1, c + 
         1).Range.Text = dataGridView1.Columns[c].HeaderText;
              }
  
              //table style
              oDoc.Application.Selection.Tables[1].set_Style("Grid Table 
          4 - Accent 5");
              oDoc.Application.Selection.Tables[1].Rows[1].Select();
              oDoc.Application.Selection.Cells.VerticalAlignment = 
            Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
  
              //header text
              foreach (Word.Section section in 
           oDoc.Application.ActiveDocument.Sections)
              {
                  Word.Range headerRange = 
           section.Headers[Word.WdHeaderFooterIndex.
                        wdHeaderFooterPrimary].Range;
                  headerRange.Fields.Add(headerRange, 
              Word.WdFieldType.wdFieldPage);
                  headerRange.Text = "Header Text";
                  headerRange.Font.Size = 16;
                  headerRange.ParagraphFormat.Alignment =  
              Word.WdParagraphAlignment.wdAlignParagraphCenter;
              }
              oDoc.Activate();
              filename = @"D:\\Save.docx";
              oDoc.SaveAs2(filename);
              oDoc.Close();
              app.Quit();
          }
           }
        private void button1_Click(object sender, EventArgs e)
        {
          Save();
        }
         }
            }`
  </code></pre>