Solved

Create a DataTable from an Excel sheet

Posted on 2014-11-26
2
108 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

810 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