title: How can I read the rows of an Excel csv or xls file into an ASP.NET app from a remote location using C#?
body: <p>Here's my situation. I'm designing a program that takes Excel files (which may be in csv, xls, or xlsx format) from a remote network drive, processes the data, then outputs and stores the results of that process. The program provides a listbox of filenames that are obtained from the remote network drive folder using the method detailed in the accepted answer <a href="https://stackoverflow.com/questions/2608194/how-do-i-start-a-thread-in-a-different-security-context">here</a>. Once the user selects a filename from the listbox, I want the program to find the file and obtain the information from it to do the data processing. I have tried using <a href="https://stackoverflow.com/a/16051/2066535">this</a> method to read the data from the Excel file while in a threaded security context, but that method just fails without giving any kind of error. It seems to not terminate. Am I going about this the wrong way?</p>
  
  <p><strong>Edit - (Final Notes: I have taken out the OleDbDataAdapter and replaced it with EPPlus handling.)</strong></p>
  
  <p>I was able to scrub sensitive data from the code, so here it is:</p>
  
  <pre><code>protected void GetFile(object principalObj)
      {
          if (principalObj == null)
          {
              throw new ArgumentNullException("principalObj");
          }
  
          IPrincipal principal = (IPrincipal)principalObj;
          Thread.CurrentPrincipal = principal;
          WindowsIdentity identity = principal.Identity as WindowsIdentity;
          WindowsImpersonationContext impersonationContext = null;
          if (identity != null)
          {
              impersonationContext = identity.Impersonate();
          }
          try
          {
              string fileName = string.Format("{0}\\" + Files.SelectedValue, @"RemoteDirectoryHere");
              string connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.14.0; data source={0}; Extended Properties=Excel 14.0;", fileName);
  
              OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM Sheet1", connectionString);
              DataSet ds = new DataSet();
  
              adapter.Fill(ds, "Sheet1");
  
              dataTable = ds.Tables["Sheet1"];
          }
          finally
          {
              if (impersonationContext != null)
              {
                  impersonationContext.Undo();
              }
          }
      }
  </code></pre>
  
  <p><strong>Additional Edit</strong></p>
  
  <p>Now xlsx files have been added to the mix.</p>
  
  <p><strong>Third Party</strong></p>
  
  <p>Third party solutions are not acceptable in this case (unless they allow unrestricted commercial use).</p>
  
  <p><strong>Attempts - (Final Notes: Ultimately I had to abandon OleDb connections.)</strong></p>
  
  <p>I have tried all of the different connection strings offered, and I have tried them with just one file type at a time. None of the connection strings worked with any of the file types.</p>
  
  <p><strong>Permissions</strong></p>
  
  <p>The User <em>does</em> have access to the file and its directory. </p>