Solved read current spreadsheet

Posted on 2012-08-13
Last Modified: 2012-08-16

How would I use code similar to that below in my Excel Add in project to read
the current Excel spreadsheet

    Protected Function ExcelConnection() As SqlCommand

            Dim sMinAndSecond As String = Me.Second_Minute_of_Upload.Text 'workaround to stop file locking on the same name

            Dim xConnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=" & Server.MapPath("~/Temp/ExcelImport" & sMinAndSecond & ".xls") & ";" & _
            "Extended Properties=""Excel 8.0;HDR=YES;IMEX=1"""

            ' create your excel connection object using the connection string
            Dim objXConn As New SqlConnection(xConnStr)

            ' use a SQL Select command to retrieve the data from the Excel Spreadsheet
            ' the "table name" is the name of the worksheet within the spreadsheet
            ' in this case, the worksheet name is "Members" and is expressed as: [Members$]
            Dim objCommand As New SqlCommand("SELECT * FROM [Sheet1$]", objXConn)
            Return objCommand
        Catch ex As Exception
            Response.Write("There was an error connecting to the Excel spreadsheet! " & Err.Description)
        End Try

    End Function
Question by:murbro
    LVL 83

    Accepted Solution

    That code is OLEDB based. In your addin, you can access the cells directly.

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now