title: Reading of Excel File c#
body: <p><img src="https://i.stack.imgur.com/8g1DD.jpg" alt="enter image description here"></p>
  
  <p>Hi guys! I encountered a problem is that I have difficulty reading this Excel file into data grid view as shown above. Any help will be greatly appreciated. Thanks.</p>
  
  <p>These were my codes to read Excel file but there's a error that I have difficulty troubleshooting it.</p>
  
  <pre><code>private void btnSearch_Click(object sender, EventArgs e)
      {
          Excel.Workbook workbook;
          Excel.Worksheet NwSheet;
          Excel.Range ShtRange;
          Microsoft.Office.Interop.Excel.Application ExcelObj = new Microsoft.Office.Interop.Excel.Application();
          OpenFileDialog filedlgExcel = new OpenFileDialog();
          filedlgExcel.Title = "Select file";
          filedlgExcel.InitialDirectory = @"c:\";
          filedlgExcel.FileName = txtFileName.Text;
          filedlgExcel.Filter = "Excel Sheet(*.xlsx)|*.xlsx|All Files(*.*)|*.*";
          filedlgExcel.FilterIndex = 1;
          filedlgExcel.RestoreDirectory = true;
          if (filedlgExcel.ShowDialog() == DialogResult.OK)
          {
             workbook = ExcelObj.Workbooks.Open(filedlgExcel.FileName);
              NwSheet = (Excel.Worksheet)workbook.Sheets.get_Item(1);
          ShtRange = NwSheet.UsedRange; //gives the used cells in sheet
          //Reading Excel file.
          //Creating dataTable to read the containt of the Sheet in File.
  
          //Set header name
          for (int Cnum = 1; Cnum &lt;= ShtRange.Columns.Count; Cnum++)
          {
              dt.Columns.Add(new DataColumn((ShtRange.Cells[1, Cnum] as Excel.Range).Value2.ToString()));
          }
          dt.AcceptChanges();
          //store coumn names to array
          string[] columnNames = (from dc in dt.Columns.Cast&lt;DataColumn&gt;() select dc.ColumnName).ToArray();
  
          //populate fields
          for (int Rnum = 2; Rnum &lt;= ShtRange.Rows.Count; Rnum++)
          {
              DataRow dr = dt.NewRow();
              for (int Cnum = 1; Cnum &lt;= ShtRange.Columns.Count; Cnum++)
              {
                  dr[Cnum - 1] = (ShtRange.Cells[Rnum, Cnum] as Excel.Range).Value2.ToString();
              }
              dt.Rows.Add(dr);
              dt.AcceptChanges();
          }
          workbook.Close(true, Missing.Value, Missing.Value);
          ExcelObj.Quit();
  
          foreach (DataRow dr in dt.Rows)
          {
              string strEmployee = dr["Employee Name"].ToString();
              obj1 = new List&lt;employeeschedule&gt;();
              for (int i = 1; i &lt; dt.Columns.Count; i++)
              {
                  string period = dr[i].ToString();
                  string[] split = period.Split('–');
                  employeeschedule es = new employeeschedule();
                  string day = columnNames[i];
                  if (split[0] == "REST")
                  {
                      es.day = day;
                      es.startTime = "0000";
                      es.endTime = "0000";
                      es.restDay = "Yes";
                  }
                  if (split[0] == "OFF")
                  {
                      es.day = day;
                      es.startTime = "0000";
                      es.endTime = "0000";
                      es.restDay = "Yes";
                  }
                  else
                  {
                      es.day = day;
                      es.startTime = split[0];
                      es.endTime = split[1];
                      es.restDay = "No";
                  }
                  obj1.Add(es);
              }
  
              dict.Add(strEmployee, obj1);
              dgvEmployeeShift.DataSource = dt;
          }
              }
          }
  </code></pre>
  
  <p>The error falls on this part:</p>
  
  <pre><code> dt.Columns.Add(new DataColumn((ShtRange.Cells[1, Cnum] as Excel.Range).Value2.ToString()));
  </code></pre>
  
  <p>It states that "Cannot perform runtime binding on a null reference".</p>