title: I am getting an error while exporting to Excel
body: <p>I want to transfer the data from dataGridView to Excel. For this, I added the &quot;Microsoft.Office.Interop.Excel&quot; plug-in to my project. I wrote the necessary codes properly. But when I run the project and click the &quot;Export to Excel&quot; button, I get an error. What should I do?</p>
  <p><strong>This is my code:</strong></p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Data.SqlClient;
  using System.Drawing;
  using System.IO;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  
  namespace DuyuTekStokApp
  {
      public partial class StockAll : Form
      {
          SqlConnection connect = new SqlConnection(&quot;Data Source=DESKTOP-L01MAVE;Initial Catalog='StockControl';Integrated Security=True&quot;);
          public StockAll()
          {
              InitializeComponent();
          }
          public void VerileriGoster(string Veriler)
          {
              SqlDataAdapter da = new SqlDataAdapter(Veriler, connect);
              DataSet ds = new DataSet();
              da.Fill(ds);
              dataGridViewAll.DataSource = ds.Tables[0];
          }
          public void changeHeader()
          {
              dataGridViewAll.Columns[0].HeaderText = &quot;Stok Kodu&quot;;
              dataGridViewAll.Columns[1].HeaderText = &quot;Stok Cinsi&quot;;
              dataGridViewAll.Columns[2].HeaderText = &quot;Barkod Numarası&quot;;
              dataGridViewAll.Columns[3].HeaderText = &quot;Birimi&quot;;
              dataGridViewAll.Columns[4].HeaderText = &quot;Grubu&quot;;
              dataGridViewAll.Columns[5].HeaderText = &quot;Tarih&quot;;
              dataGridViewAll.Columns[6].HeaderText = &quot;Açıklama&quot;;
              dataGridViewAll.Columns[7].HeaderText = &quot;Sınıfı&quot;;
              dataGridViewAll.Columns[8].HeaderText = &quot;Değişim Tarihi&quot;;
              dataGridViewAll.Columns[9].HeaderText = &quot;Adeti&quot;;
              dataGridViewAll.Columns[10].HeaderText = &quot;Kritik Miktar&quot;;
              dataGridViewAll.Columns[11].HeaderText = &quot;Resim&quot;;
          }
          private void StockAll_Load(object sender, EventArgs e)
          {
              connect.Open();
              SqlCommand cmd1 = new SqlCommand(&quot;SELECT COUNT(*) FROM StockCard&quot;, connect);
              SqlDataReader dr1 = cmd1.ExecuteReader();
              while (dr1.Read())
              {
                  labelToplam.Text = dr1[0].ToString();
              }
              connect.Close();
  
              connect.Open();
              SqlCommand cmd2 = new SqlCommand(&quot;SELECT SUM(StokAdet) FROM StockCard&quot;, connect);
              SqlDataReader dr2 = cmd2.ExecuteReader();
              while (dr2.Read())
              {
                  labelGenel.Text = dr2[0].ToString();
              }
              connect.Close();
  
              VerileriGoster(&quot;SELECT StokKodu, StokCinsi, StokBarkod, StokBirim, StokGrup, StokTarih, StokAciklama, StokSinif, StokSonTarih, StokAdet, StokKritik, StokResim FROM StockCard ORDER BY StokKodu ASC&quot;);
              //VerileriGoster(@&quot;SELECT dbo.StockCard.StokKodu, dbo.StockCard.StokBarkod, dbo.StockCard.StokBirim, dbo.StockCard.StokGrup, dbo.StockCard.StokTarih, dbo.StockCard.StokResim, dbo.StockMove.MoveAdet, dbo.StockMove.MoveMevcut, dbo.StockMove.MoveSinif, dbo.StockMove.MoveTarih
              //                     FROM  dbo.StockMove INNER JOIN
              //                  dbo.StockCard ON dbo.StockMove.StokID = dbo.StockCard.StokID ORDER BY StokKodu ASC &quot;);
              changeHeader();
          }
  
          private void dataGridViewAll_CellClick(object sender, DataGridViewCellEventArgs e)
          {
              int secilen = dataGridViewAll.SelectedCells[0].RowIndex;
              labelUrun.Text = dataGridViewAll.Rows[secilen].Cells[9].Value?.ToString();
              labelKrit.Text = dataGridViewAll.Rows[secilen].Cells[10].Value.ToString();
              byte[] bytes = (byte[])dataGridViewAll.Rows[secilen].Cells[11].Value;
              MemoryStream ms = new MemoryStream(bytes);
              pictureBoxResim.Image = Image.FromStream(ms);
          }
  
          private void btnExcel_Click(object sender, EventArgs e)
          {
              Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
              Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
              Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
              worksheet = workbook.Sheets[&quot;Sheet1&quot;];
              worksheet = workbook.ActiveSheet;
              worksheet.Name = &quot;Stok Bilgisi&quot;;
  
              for (int i = 1; i &lt; dataGridViewAll.Columns.Count + 1; i++)
              {
                  worksheet.Cells[i, 1] = dataGridViewAll.Columns[i - 1].HeaderText;
              }
  
              for (int i = 0; i &lt; dataGridViewAll.Rows.Count; i++)
              {
                  for (int j = 0; j &lt; dataGridViewAll.Columns.Count; i++)
                  {
                      worksheet.Cells[i + 2, j + 1] = dataGridViewAll.Rows[i].Cells[j].Value.ToString();
                  }
              }
  
              var saveFileDialog = new SaveFileDialog();
              saveFileDialog.FileName = &quot;StokBilgisi&quot;;
              saveFileDialog.DefaultExt = &quot;.xlsx&quot;;
              if (saveFileDialog.ShowDialog() == DialogResult.OK)
              {
                  workbook.SaveAs(saveFileDialog.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
              }
              app.Quit();
  
              
          }
      }
  }
  </code></pre>
  <p><strong>This is the error message I get:</strong></p>
  <pre><code>System.InvalidCastException
    HResult = 0x80004002
    Message = COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' could not be assigned to interface type 'Microsoft.Office.Interop.Excel._Application'. This operation failed because the QueryInterface call on the COM component for the interface with the IID '{000208D5-0000-0000-C000-000000000046}' failed with the following error: Error loading type library / DLL. (HRESULT returned exception: 0x80029C4A (TYPE_E_CANTLOADLIBRARY)).
    Source = mscorlib
    StackTrace:
     at System.StubHelpers.StubHelpers.GetCOMIPFromRCW (Object objSrc, IntPtr pCPCMD, IntPtr &amp; ppTarget, Boolean &amp; pfNeedsRelease)
     at Microsoft.Office.Interop.Excel.ApplicationClass.get_Workbooks ()
     at SenseTekStokApp.StockAll.btnExcel_Click (Object sender, EventArgs e) in D: \ yNs \ VisualTutorials \ SenseTekStokApp \ SenseTekStokApp \ StockAll.cs: line 84
     at System.Windows.Forms.Control.OnClick (EventArgs e)
     at System.Windows.Forms.Button.OnClick (EventArgs e)
     at System.Windows.Forms.Button.OnMouseUp (MouseEventArgs mevent)
     at System.Windows.Forms.Control.WmMouseUp (Message &amp; m, MouseButtons button, Int32 clicks)
     at System.Windows.Forms.Control.WndProc (Message &amp; m)
     at System.Windows.Forms.ButtonBase.WndProc (Message &amp; m)
     at System.Windows.Forms.Button.WndProc (Message &amp; m)
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage (Message &amp; m)
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc (Message &amp; m)
     at System.Windows.Forms.NativeWindow.DebuggableCallback (IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
     at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW (MSG &amp; msg)
     at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop (IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner (Int32 reason, ApplicationContext context)
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoop (Int32 reason, ApplicationContext context)
     at System.Windows.Forms.Application.Run (Form mainForm)
     at SenseTekStokApp.Program.Main () in D: \ yNs \ VisualTutorials \ SenseTekStokApp \ SenseTekStokApp \ Program.cs: line 19
  </code></pre>