title: Export to DataGrid To Excel in WPf
body: <p>I want export to datagrid to wpf but when export an error is generated </p>
  
  <p>An unhandled exception of type 'System.NullReferenceException' occurred in solutions.exe</p>
  
  <p>my code...</p>
  
  <pre><code>    Excel.Application excel = new Excel.Application();
  
          excel.Visible = true; 
          Workbook workbook = excel.Workbooks.Add(System.Reflection.Missing.Value);
          Worksheet sheet1 = (Worksheet)workbook.Sheets[1];
          Range range;
          Range myRange;
          for (int i = 1; i &lt; dgDatos.Columns.Count; i++)
          {
               range = (Range)sheet1.Cells[1, i + 1];
              sheet1.Cells[1, i + 1].Font.Bold = true;
              range.Value = dgDatos.Columns[i].Header;
  
              for (int j = 0; j &lt; dgDatos.Items.Count; j++)
              {
                  TextBlock b = dgDatos.Columns[i].GetCellContent(dgDatos.Items[j]) as TextBlock;
                   myRange = sheet1.Cells[j + 2, i + 1];
                  myRange.Value = b.Text;
              }
          }
  </code></pre>
  
  <p>}</p>