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. Import Excel sheet into Gridview and SQL

Import Excel sheet into Gridview and SQL

Scheduled Pinned Locked Moved Web Development
csharpdatabasevisual-studioalgorithms
9 Posts 6 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.
  • M Offline
    M Offline
    Michael Hinkle
    wrote on last edited by
    #1

    I am needing to import excel data into my program. I am using Visual Studio 2019 web development and my code behind runs VB. I would like to load data into gridview and from there into sql. I have a lot of work to do to clean up the data before my final product is ready. So I am wiring a routing to clean up the data. Anyone have an idea. I have been searching online but most of the options does not work. If you have the answer please also provide me with the imports statements I need.

    L Richard DeemingR M D S 5 Replies Last reply
    0
    • M Michael Hinkle

      I am needing to import excel data into my program. I am using Visual Studio 2019 web development and my code behind runs VB. I would like to load data into gridview and from there into sql. I have a lot of work to do to clean up the data before my final product is ready. So I am wiring a routing to clean up the data. Anyone have an idea. I have been searching online but most of the options does not work. If you have the answer please also provide me with the imports statements I need.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      It is not clear what your actual problem is, but importing from Excel is fairly painless in .NET. You just need to use the Microsoft.Office.Interop.Excel Namespace | Microsoft Docs[^]. Google will find you sample code. [edit] As @RichardDeeming points out below, you cannot do this in a web application. But you could do it offline if that is an option. [/edit]

      Richard DeemingR M 2 Replies Last reply
      0
      • L Lost User

        It is not clear what your actual problem is, but importing from Excel is fairly painless in .NET. You just need to use the Microsoft.Office.Interop.Excel Namespace | Microsoft Docs[^]. Google will find you sample code. [edit] As @RichardDeeming points out below, you cannot do this in a web application. But you could do it offline if that is an option. [/edit]

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        You can't use Excel interop in a web application. :)

        Considerations for server-side Automation of Office[^]

        Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        L 1 Reply Last reply
        0
        • M Michael Hinkle

          I am needing to import excel data into my program. I am using Visual Studio 2019 web development and my code behind runs VB. I would like to load data into gridview and from there into sql. I have a lot of work to do to clean up the data before my final product is ready. So I am wiring a routing to clean up the data. Anyone have an idea. I have been searching online but most of the options does not work. If you have the answer please also provide me with the imports statements I need.

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          Michael Hinkle wrote:

          web development

          You won't be able to use Excel automation to do this:

          Considerations for server-side Automation of Office[^]

          Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

          Instead, use a library which supports reading Excel files without having Excel installed. For Excel 2007 files (*.xlsx), any of the following should work:

          • ClosedXML[^];
          • ExcelDataReader[^];
          • The OpenXML SDK[^];

          If you need to support old-format files (*.xls), then you can use NPOI[^].


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            You can't use Excel interop in a web application. :)

            Considerations for server-side Automation of Office[^]

            Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Do you ever find that someone unplugged your brain while you were asleep? :( TBH I missed the two key words.

            1 Reply Last reply
            0
            • L Lost User

              It is not clear what your actual problem is, but importing from Excel is fairly painless in .NET. You just need to use the Microsoft.Office.Interop.Excel Namespace | Microsoft Docs[^]. Google will find you sample code. [edit] As @RichardDeeming points out below, you cannot do this in a web application. But you could do it offline if that is an option. [/edit]

              M Offline
              M Offline
              Michael Hinkle
              wrote on last edited by
              #6

              THe only purpose in me doing this is to get my data in to sql so that I can start cleaning the old data up (It is a mess), and making the needed changes. The only time I would use this is a few times when I need to reupload data.

              1 Reply Last reply
              0
              • M Michael Hinkle

                I am needing to import excel data into my program. I am using Visual Studio 2019 web development and my code behind runs VB. I would like to load data into gridview and from there into sql. I have a lot of work to do to clean up the data before my final product is ready. So I am wiring a routing to clean up the data. Anyone have an idea. I have been searching online but most of the options does not work. If you have the answer please also provide me with the imports statements I need.

                M Offline
                M Offline
                Mycroft Holmes
                wrote on last edited by
                #7

                Assuming SQL means SQl server the why not do the clean up in SSMS. My standard method was: Import the data manually into SQL Server. I would load the data into a new table accepting whatever garbage is in the excel file. All data should be converted to strings on the way in. Create and empty destination table matching your final destination (this is so you can repeat the process) Write a stored procedure that: loops each column and cleans and formats the data as required and inserts it into the temp table. When you are confident the process works then change the destination to the final destination table. If this works consistently between loads you can then turns the entire process into a Job which can then be launched by your application. This method does require reasonable T-SQL skills but would also be an excellent learning exercise.

                Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP

                1 Reply Last reply
                0
                • M Michael Hinkle

                  I am needing to import excel data into my program. I am using Visual Studio 2019 web development and my code behind runs VB. I would like to load data into gridview and from there into sql. I have a lot of work to do to clean up the data before my final product is ready. So I am wiring a routing to clean up the data. Anyone have an idea. I have been searching online but most of the options does not work. If you have the answer please also provide me with the imports statements I need.

                  D Offline
                  D Offline
                  David Mujica
                  wrote on last edited by
                  #8

                  Here is something that I use in my web app ... 1) Define a connection string pointing to the Excel file

                  String.Format("Provider=Microsoft.ACE.OLEDB.12.0; Data Source = {0}; Extended Properties='Excel 12.0; HDR=Yes; IMEX=1;'", sFilename)

                  1. You can access some schema information from the Excel doc like this ...

                  DTschema = dbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)

                  1. Then get to the actual data in the spreadsheet like this ...

                  Using dbCmd As New OleDbCommand("SELECT * FROM [" & sSheet & "]", dbConn)
                  Using dbAdapter As New OleDbDataAdapter(dbCmd)
                  dbAdapter.Fill(DT)
                  End Using

                  It may not be pretty, but you get the data from the spreadsheet into a datatable which can then be bound to a Gridview. See if it works for you. :thumbsup:

                  1 Reply Last reply
                  0
                  • M Michael Hinkle

                    I am needing to import excel data into my program. I am using Visual Studio 2019 web development and my code behind runs VB. I would like to load data into gridview and from there into sql. I have a lot of work to do to clean up the data before my final product is ready. So I am wiring a routing to clean up the data. Anyone have an idea. I have been searching online but most of the options does not work. If you have the answer please also provide me with the imports statements I need.

                    S Offline
                    S Offline
                    SeeSharp2
                    wrote on last edited by
                    #9

                    You can use the Open XML SDK 2.5 for Office | Microsoft Docs[^] to create and or read spreadsheets.

                    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