title: How do you programmatically check if a spreadsheet has headers in C#
body: <p>I am creating a winform application where every day, a user will select a xlsx file with the day's shipping information to be merged with our invoicing data. </p>
  
  <p>The challenge I am having is when the user does not download the xlsx file with the specification that the winform data requires. (I wish I  could eliminate this step with an API connection but sadly I cannot)</p>
  
  <p>My first step is checking to see if the xlsx file has headers to that my file path is valid </p>
  
  <p>Example</p>
  
  <pre><code>string connString = "provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + *path* + "';Extended Properties='Excel 12.0;HDR=YES;';"; 
  </code></pre>
  
  <p>Where <em>path</em> is returned from an OpenFileDialog box</p>
  
  <p>If the file was chosen wasn't downloaded with headers the statement above throws an exception. </p>
  
  <p>If change HDR=YES; to HDR=NO; then I have trouble identifying the columns I need and if the User bothered to include the correct ones.</p>
  
  <p>My code then tries to load the data into a DataTable</p>
  
  <pre><code>    private void loadRows() 
      {
          for (int i = 0; i &lt; deliveryTable.Rows.Count; i++)
          {
              DataRow dr = deliveryTable.Rows[i];
              int deliveryId = 0;
              bool result = int.TryParse(dr[0].ToString(), out deliveryId);
              if (deliveryId &gt; 1 &amp;&amp; !Deliveries.ContainsKey(deliveryId))
              {
                  var delivery = new Delivery(deliveryId)
                  {
                      SalesOrg = Convert.ToInt32(dr[8]),
                      SoldTo = Convert.ToInt32(dr[9]),
                      SoldName = dr[10].ToString(),
                      ShipTo = Convert.ToInt32(dr[11]),
                      ShipName = dr[12].ToString(),
                  };
  </code></pre>
  
  <p>Which all works only if the columns are in the right place.
  If they are not in the right place my thought is to display a message to the user to get the right information</p>
  
  <p>Does anyone have any suggestions?
  (Sorry, first time posting a question and still learning to think through it)</p>