title: How to take Excel file uploaded from website and import data to SQL Server programmatically?
body: <p>I'm trying to do something like this:</p>
  
  <pre><code>public void ImportClick(object sender, EventArgs e) //the button used after selecting the spreadsheet file
  {
      if (fileUpload.HasFile) //ASP.Net FileUpload control
      {
          if (fileUpload.FileName.EndsWith(".xls", StringComparison.OrdinalIgnoreCase) || fileUpload.FileName.EndsWith(".xlsx", StringComparison.OrdinalIgnoreCase))
          {
              Excel sheet = new Excel(fileUpload.Open()); //not sure how to do this part
  
              using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["our_database"].ConnectionString))
              {
                  using (SqlCommand command = new SqlCommand("INSERT INTO table_name SELECT * FROM " + sheet, connection))
                  {
                      connection.Open();
                      command.ExecuteQuery(); //Intellisense only has "ExecuteNonQuery()" method, but that's for T-SQL
                      connection.Close();
                  }
              }
          }
          else
          {
              error.Text = "File must be either *.xls or *.xlsx";
              error.Visible = true;
          }
      }
      else
      {
          error.Text = "No file was selected";
          error.Visible = true;
      }
  }
  </code></pre>
  
  <p>There are a lot of classes and interfaces in the <a href="http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel%28v=office.11%29.aspx" rel="nofollow">Microsoft.Office.Interop.Excel namespace</a>, and I don't know which one to use.</p>
  
  <p>I know making the Excel object, along with the SQL command, probably won't be as easy as what I have here, but those are the two things I need help with.</p>
  
  <p>Any suggestions/advice would be greatly appreciated!</p>