title: Reading a spreadsheetl file from fileupload control into filestream
body: <p>I am trying to find the best way of selecting an excel spreadsheet (xls and xlsx) using a fileupload control and then parsing it with filestream object and then populating a dataset.</p>
  
  <p>This is my code so far which does the job but how it differs is I am saving the excel spreadsheet to a folder in my solution then querying the data using Microsoft ACE OLEDB connection.</p>
  
  <pre><code>protected void btnUpload_Click(object sender, EventArgs e)
      {
          string connectingString = "";
          if (ctrlFileUpload.HasFile)
          {
              string fileName =
                  Path.GetFileName(ctrlFileUpload.PostedFile.FileName);
  
              string fileExtension =
                  Path.GetExtension(ctrlFileUpload.PostedFile.FileName);
                  //Path.GetExtension(ctrlFileUpload.PostedFile.ContentType);
  
              string fileLocation =
                  Server.MapPath("~/App_Data/" + fileName);
              ctrlFileUpload.SaveAs(fileLocation);
  
              // Check whether file extension is xls or xslx
              if (fileExtension == ".xls")
              {
                  connectingString =
                      "Provider=Microsoft.ACE.OLEDB.4.0;Data Source=" +
                      fileLocation + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=2\"";
              }
  
              else if (fileExtension == ".xlsx")
              {
                  connectingString =
                      "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                      fileLocation + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=2\"";
              }
  
              // Create OleDb Connection and OleD Command
              using (OleDbConnection con = new OleDbConnection(connectingString))
              {
                  try
                  {
                      OleDbCommand cmd = new OleDbCommand();
                      cmd.CommandType = CommandType.Text;
                      cmd.Connection = con;
                      OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
                      //DataTable dtExcelRecords = new DataTable();
                      DataSet ds = new DataSet();
                      con.Open();
                      DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
  
                      string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
                      cmd.CommandText = "Select * FROM [" + getExcelSheetName + "]";
                      dAdapter.SelectCommand = cmd;
                      //dAdapter.Fill(dtExcelRecords);
                      dAdapter.Fill(ds);
                      //GridView1.DataSource = dtExcelRecords;
                      //GridView1.DataBind();
                  }
                  catch (Exception ex)
                  {
  
                  }
              }
  
          }
      }
  </code></pre>
  
  <p>So to summarise I want to read the data in a spreadsheet and then bind it to a dataset but without saving the file to a physical path.</p>
  
  <p>Is there a cleaner way of writing this code that I am missing. Thanks!!</p>