Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to upload Excel file to Database from ASP.NET

How to upload Excel file to Database from ASP.NET

Scheduled Pinned Locked Moved ASP.NET
csharpdatabaseasp-netsql-serversysadmin
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cheguri
    wrote on last edited by
    #1

    Dear friends, I want to upload an Excel file from ASP.NET web form to Sql Server Database. I have all employees salary in the excel file. I want to upload it to the database through ASP.NET web form. I am using ASP.NET 3.5 with C# and SQL Server 2005. Please, anyone can help me, How to upload excel file using the FileUpload control? I need it urgently. Please help me. Regards, Dileep.

    G 1 Reply Last reply
    0
    • C cheguri

      Dear friends, I want to upload an Excel file from ASP.NET web form to Sql Server Database. I have all employees salary in the excel file. I want to upload it to the database through ASP.NET web form. I am using ASP.NET 3.5 with C# and SQL Server 2005. Please, anyone can help me, How to upload excel file using the FileUpload control? I need it urgently. Please help me. Regards, Dileep.

      G Offline
      G Offline
      Gaurav Dudeja India
      wrote on last edited by
      #2

      use this function

      public static DataTable GetDataTableFromExcel(string SourceFilePath)
      {
      string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
      "Data Source=" + SourceFilePath + ";" +
      "Extended Properties=Excel 8.0;";

      using (OleDbConnection cn = new OleDbConnection(ConnectionString))
      {
          cn.Open();
      
          DataTable dbSchema = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
          if (dbSchema == null || dbSchema.Rows.Count < 1)
          {
                  throw new Exception("Error: Could not determine the name of the first worksheet.");
          }
      
          string WorkSheetName = dbSchema.Rows\[0\]\["TABLE\_NAME"\].ToString();
      
          OleDbDataAdapter da = new OleDbDataAdapter("SELECT \* FROM \[" + WorkSheetName + "\]", cn);
          DataTable dt = new DataTable(WorkSheetName);
      
          da.Fill(dt);
      
          return dt;
      }
      

      }

      This will return you datatable after that with the help of loop insert it into database

      Gaurav Dudeja http://www.gdinfotechindia.com
      Dont be afraid of changing your life to better !

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups