Solved

Create a DataTable from an Excel sheet

Posted on 2014-11-26
2
105 Views
Last Modified: 2014-11-27
Hi

The following code is used to create a DataTable in Eric Moreau's post at
http://emoreau.com/Entries/Articles/2010/12/Using-ADONet-SQL-Bulk-Copy-feature.aspx
I am trying to work out how to create a the same table from a spreadsheet like the one shown in the image
How would I do this?

    Private Function CreateDataSource(ByVal pLargeDataTable As Boolean) As DataTable
        Dim dt As New DataTable("Person")
        With dt
            .Columns.Add("ID", GetType(Integer))
            .Columns.Add("FirstName", GetType(String))
            .Columns.Add("LastName", GetType(String))

            'Add rows
            .LoadDataRow(New Object() {-1, "Joe", "Dalton"}, True)
            .LoadDataRow(New Object() {-2, "Jack", "Dalton"}, True)
            .LoadDataRow(New Object() {-3, "Willam", "Dalton"}, True)
            .LoadDataRow(New Object() {-4, "Averell", "Dalton"}, True)

            If pLargeDataTable Then
                Dim intID As Integer = 5
                Do While intID <= 100000
                    .LoadDataRow(New Object() {intID * -1, "FN" + intID.ToString, "Dalton"}, True)
                    intID += 1
                Loop
            End If
        End With

        Return dt
    End Function

1
0
Comment
Question by:murbro
2 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
ID: 40469085
Are you trying to create the spreadsheet?

Something like this:

    Function CreateDataSource(ByVal pLargeDataTable As Boolean)
        xlApp = CreateObject("Excel.Application")
        xlApp.Workbooks.Add()
        xlApp.Visible = True
        introw = 1
        With xlApp.activeworkbook.sheets(1)
            Call AddToExcel(-1, "Joe", "Dalton")
            Call AddToExcel(-2, "Jack", "Dalton")
            Call AddToExcel(-3, "Willam", "Dalton")
            Call AddToExcel(-4, "Averell", "Dalton")
            If pLargeDataTable Then
                Do While introw <= 100000
                    Call AddToExcel(introw * -1, "FN" + introw.ToString, "Dalton")
                Loop
            End If
        End With
        CreateDataSource = xlApp
    End Function
    Sub AddToExcel(ByVal ParamArray ToAdd())
        xlApp.range("A" & introw & ":C" & introw).value = ToAdd
        introw += 1
    End Sub

Open in new window

0
 

Author Closing Comment

by:murbro
ID: 40469168
Yes. Thanks
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

16 Experts available now in Live!

Get 1:1 Help Now