title: How to Export DataGridView to PDFth/Excel/Word for Easy Print?
body: <p>Guys what I have is a simple gridview in C# windows form application which is being populated by a database. Now I want to add a functionality to print it. I googled and tried different methods but none have worked so far (Some printed blank pages, while other misprinted the data). </p>
  
  <p>This is the code which gave me the best result (it doesn't print all the records which can be viewed by scroll)..</p>
  
  <pre><code>    private void button1_Click(object sender, EventArgs e)
      {
          PrintDialog printDialog = new PrintDialog();
          printDialog.Document = printDocument1;
          printDialog.UseEXDialog = true;
          if (DialogResult.OK == printDialog.ShowDialog())
          {
              printDocument1.DocumentName = "Test Page Print";
              printDocument1.Print();
          }
      }
  
      private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
      {
          Bitmap bm = new Bitmap(this.dgRecords.Width, this.dgRecords.Height);
          this.dgRecords.DrawToBitmap(bm, new Rectangle(0, 0, this.dgRecords.Width, this.dgRecords.Height));
          e.Graphics.DrawImage(bm, 0, 0);
      }
  </code></pre>
  
  <p>Now I thought maybe the work-around was that if I could export the datagridview to any other file such as excel sheet, pdf or word document and then I could print it from that file. I have tried to export it to excel but haven't been successful. It just prints header and one row.</p>
  
  <p>This is the code for excel-export </p>
  
  <pre><code>    private void ToCsV(DataGridView dGV, string filename)
      {
          string stOutput = "";
          string sHeaders = "";
          for (int j = 0; j &lt; dGV.Columns.Count; j++)
              sHeaders = sHeaders.ToString() + Convert.ToString(dGV.Columns[j].HeaderText) + "\t";
          stOutput += sHeaders + "\r\n";
          for (int i = 0; i &lt; dGV.RowCount - 1; i++)
          {
              string stLine = "";
              for (int j = 0; j &lt; dGV.Rows[i].Cells.Count; j++)
                  stLine = stLine.ToString() + Convert.ToString(dGV.Rows[i].Cells[j].Value) + "\t";
              stOutput += stLine + "\r\n";
          }
          Encoding utf16 = Encoding.GetEncoding(1254);
          byte[] output = utf16.GetBytes(stOutput);
          FileStream fs = new FileStream(filename, FileMode.Create);
          BinaryWriter bw = new BinaryWriter(fs);
          bw.Write(output, 0, output.Length); //write the encoded file
          bw.Flush();
          bw.Close();
          fs.Close();
      }  
  
      private void button3_Click(object sender, EventArgs e)
      {
          SaveFileDialog sfd = new SaveFileDialog();
          sfd.Filter = "Excel Documents (*.xls)|*.xls";
          sfd.FileName = "export.xls";
          if (sfd.ShowDialog() == DialogResult.OK)
          {
              ToCsV(dgRecords, sfd.FileName);  
          }  
      }
  </code></pre>
  
  <p>Could some one tell me what am I doing wrong? I just need to print the gridview - doesn't matter if it is directly by the print button or by export it to some other file.</p>
  
  <p>EDIT :</p>
  
  <pre><code>        Microsoft.Office.Interop.Excel.Application ExcelApp =
                   new Microsoft.Office.Interop.Excel.Application();
          Microsoft.Office.Interop.Excel._Workbook ExcelBook;
          Microsoft.Office.Interop.Excel._Worksheet ExcelSheet;
  
          int i = 0;
          int j = 0;
  
          //create object of excel
          ExcelBook = (Microsoft.Office.Interop.Excel._Workbook)ExcelApp.Workbooks.Add(1);
          ExcelSheet = (Microsoft.Office.Interop.Excel._Worksheet)ExcelBook.ActiveSheet;
          //export header
          for (i = 1; i &lt;= this.dataGridView1.Columns.Count; i++)
          {
              ExcelSheet.Cells[1, i] = this.dataGridView1.Columns[i - 1].HeaderText;
          }
  
          //export data
          for (i = 1; i &lt;= this.dataGridView1.RowCount; i++)
          {
              for (j = 1; j &lt;= dataGridView1.Columns.Count; j++)
              {
                  ExcelSheet.Cells[i + 1, j] = dataGridView1.Rows[i - 1].Cells[j - 1].Value;
              }
          }
  
          ExcelApp.Visible = true;
  
          //set font Khmer OS System to data range
              Microsoft.Office.Interop.Excel.Range myRange = ExcelSheet.get_Range(
                  ExcelSheet.Cells[1, 1],
                  ExcelSheet.Cells[this.dataGridView1.RowCount + 1,
                      this.dataGridView1.Columns.Count]);
  
          Microsoft.Office.Interop.Excel.Font x = myRange.Font;
          x.Name = "Arial";
          x.Size = 10;
  
          //set bold font to column header
          myRange = ExcelSheet.get_Range(ExcelSheet.Cells[1, 1],
                                   ExcelSheet.Cells[1, this.dataGridView1.Columns.Count]);
          x = myRange.Font;
          x.Bold = true;
          //autofit all columns
          myRange.EntireColumn.AutoFit();
  
          //
          ExcelSheet = null;
          ExcelBook = null;
          ExcelApp = null;
  </code></pre>
  
  <p>Okay so I tried this method of export to excel and it works fine, it opens up a excel sheet with all the data from my datagrid but after opening up the excel sheet, it generates RuntimeBinder Exception</p>
  
  <pre><code>        'object' does not contain a definition for 'get_Range'
  </code></pre>
  
  <p>at this line </p>
  
  <pre><code>        Microsoft.Office.Interop.Excel.Range myRange = ExcelSheet.get_Range(
                  ExcelSheet.Cells[1, 1],
                  ExcelSheet.Cells[this.dataGridView1.RowCount + 1,
                      this.dataGridView1.Columns.Count]);
  </code></pre>
  
  <p>What seems to be the problem? I can't figure it out...</p>