title: C# win form, trying to pull data from excel, keeps saying cant find column outside of my range
body: <p>ive been tinkering with this for a few hours and im just not advanced enough to understand whats going on. i just started in C# a few weeks back. reading through a very good book. kinda jumping ahead of myself to develop this app. anyways i have data in excel about 24 columns, and its an ever expanding row length. currently at 400~ but expect it to go over 1000.
  anyways the reason it doesnt run i think is because the excel sheet uses powerquery to read some json files so alot of the cells are null. this code i reworked that i found online checks for null and i believe thats causing the issues. not sure how else i can have a variable row data table without checking for null cells though..</p>
  <pre><code>private void mainForm_Load(object sender, EventArgs e)
      {
  
  
          string file = @&quot;C:\Users\User\Desktop\test inventory\TIS.xlsm&quot;; //variable for the Excel File Location
          DataTable dt = new DataTable(); //container for our excel data
          DataRow row;
              try
              {
                  //Create Object for Microsoft.Office.Interop.Excel that will be use to read excel file
  
                  Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
  
                  Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(file);
  
                  Microsoft.Office.Interop.Excel._Worksheet excelWorksheet = excelWorkbook.Sheets[1];
                  Microsoft.Office.Interop.Excel.Range excelRange = excelWorksheet.UsedRange;
  
                  int rowCount = excelRange.Rows.Count; //get row count of excel data
                  //int colCount = excelRange.Columns.Count;//number of columns to display
                  //Get the first Column of excel file which is the Column Name
                  for (int i = 2; i &lt;= rowCount;)
                  {
                      for (int j = 1; j &lt;= 24; j++)
                      {
                          dt.Columns.Add(excelRange.Cells[i, j].Value2.ToString());
                      }
                      break;
                  }
  
                  //Get Row Data of Excel
                  int rowCounter; //This variable is used for row index number
                  for (int i = 3; i &lt;= rowCount; i++) //Loop for available row of excel data
                  {
                      row = dt.NewRow(); //assign new row to DataTable
                      rowCounter = 0;
                      for (int j = 1; j &lt;= 24; j++) //Loop for available column of excel data
                      {
                      //check if cell is empty
                      if (excelRange.Cells[i, j] != null &amp;&amp; excelRange.Cells[i, j].Value2 != null)
                          {
                              row[rowCounter] = excelRange.Cells[i, j].Value2.ToString();
                          }
                          else
                          {
                              row[i] = &quot;&quot;;
                          }
                          rowCounter++;
                      }
                      dt.Rows.Add(row); //add row to DataTable
                  }
                  dt.Columns.Remove(&quot;Description2&quot;);
                  dt.Columns.Remove(&quot;Last Inv Date&quot;);
                  dt.Columns.Remove(&quot;Quantity&quot;);
                  dt.Columns.Remove(&quot;U/M&quot;);
                  dt.Columns.Remove(&quot;Per&quot;);
                  dt.Columns.Remove(&quot;OAL2&quot;);
              dt.Columns.Remove(&quot;Product ID&quot;);
              this.dt.DataSource = dt; //assign DataTable as Datasource for DataGridview
  
                  //close and clean excel process
                  GC.Collect();
                  GC.WaitForPendingFinalizers();
                  Marshal.ReleaseComObject(excelRange);
                  Marshal.ReleaseComObject(excelWorksheet);
                  //quit apps
                  excelWorkbook.Close();
                  Marshal.ReleaseComObject(excelWorkbook);
                  excelApp.Quit();
                  Marshal.ReleaseComObject(excelApp);
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.Message);
              }
      }
  </code></pre>