title: Transfering data from one excel file to another
body: <p>I have an accounting program that generates reports and saves them in excel file. But I don't need most of the columns in this report. So I created a program that deletes the columns that I don't need. In the converted file I use 3 empty columns to write notes, data etc. I generate this report every week, so when the new report is generated I need to convert him and transfer the 3 columns, that I wrote in the old report, to the new one. The first column of the report is UserID, so I need to transfer the notes and data that I wrote for the ID's. Some ID's from the old report may be missing in the new one.
  I created a Windows Forms application that should do this, but it doesn't work good. The new report is converted, but the data from the old one is not transfered. Any ideas?? This is my code:</p>
  
  <pre><code>                OpenExcelWorkbook(OldFilePath);
                  _sheet = (Excel.Worksheet)_sheets[1];
                  _sheet.Select(Type.Missing);
  
                  var oldData = new DataTable();
                  oldData.Columns.Add("Id", typeof(string));
                  oldData.Columns.Add("Comment1", typeof(string));
                  oldData.Columns.Add("Comment2", typeof(string));
                  oldData.Columns.Add("Comment3", typeof(string));
  
                  var range = _sheet.UsedRange;
  
                  for (var i = 1; i &lt;= range.Rows.Count; i++)
                  {
                      oldData.Rows.Add(_sheet.Range["A" + i, Type.Missing].Value2, _sheet.Range["J" + i, Type.Missing].Value2, _sheet.Range["K" + i, Type.Missing].Value2,
                          _sheet.Range["L" + i, Type.Missing].Value2);
                  }
  
                  _book.Save();
                  _book.Close(false, Type.Missing, Type.Missing);
                  _app.Quit();
                  releaseObject(_sheet);
                  releaseObject(_book);
                  releaseObject(_app);
  
  
                  OpenExcelWorkbook(NewFilePath);
                  _sheet = (Excel.Worksheet)_sheets[1];
                  _sheet.Select(Type.Missing);
  
                  range = _sheet.Range["D:E", Type.Missing];
                  range.EntireColumn.Delete(Type.Missing);
                  range = _sheet.Range["E:F", Type.Missing];
                  range.EntireColumn.Delete(Type.Missing);
                  range = _sheet.Range["F:I", Type.Missing];
                  range.EntireColumn.Delete(Type.Missing);
  
                  _sheet.Cells[4, 3] = "Invoice";
                  _sheet.Cells[4, 4] = "Transfer";
                  _sheet.Cells[4, 5] = "Receipt";
  
  
                  range = _sheet.UsedRange;
  
                  var convDataTable = new DataTable();
                  convDataTable.Columns.Add("Id", typeof(string));
                  convDataTable.Columns.Add("Comment1", typeof(string));
                  convDataTable.Columns.Add("Comment2", typeof(string));
                  convDataTable.Columns.Add("Comment3", typeof(string));
  
  
                  for (var i = 1; i &lt; range.Rows.Count; i++)
                      convDataTable.Rows.Add(_sheet.Range["A" + i, Type.Missing].Value2, "", "", "");
  
                  foreach (DataRow row in convDataTable.Rows)
                  {
                      foreach (DataRow row1 in oldData.Rows)
                      {
                          if (row["Id"] == row1["Id"])
                          {
                              row["Comment1"] = row1["Comment1"];
                              row["Comment2"] = row1["Comment2"];
                              row["Comment3"] = row1["Comment3"];
                          }
                      }
                  }
  
  
                  for (var i = 1; i &lt;= convDataTable.Rows.Count; i++)
                  {
                      _sheet.Cells[i, 10] = convDataTable.Rows[i - 1]["Comment1"];
                      _sheet.Cells[i, 11] = convDataTable.Rows[i - 1]["Comment2"];
                      _sheet.Cells[i, 12] = convDataTable.Rows[i - 1]["Comment3"];
                  }
  
  
                  _book.Save();
                  _book.Close(false, Type.Missing, Type.Missing);
                  _app.Quit();
                  releaseObject(_sheet);
                  releaseObject(_book);
                  releaseObject(_app);
  </code></pre>
  
  <p>These are the functions that I use.</p>
  
  <pre><code>    private static void releaseObject(object obj)
      {
          try
          {
              System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
              obj = null;
          }
          catch (Exception ex)
          {
              obj = null;
              MessageBox.Show("Unable to release the Object " + ex.ToString());
          }
          finally
          {
              GC.Collect();
          }
      }
      protected void OpenExcelWorkbook(string filePath)
      {
          _app = new Application();
  
          _books = _app.Workbooks;
          _book = _books.Open(filePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
          _sheets = _book.Worksheets;
  
      }
  </code></pre>