Solved

Help with loading data from excel file by workbook's name

Posted on 2014-11-05
3
201 Views
Last Modified: 2014-11-06
Hi,

How do I modify the code below to load my DataGrids with multiple workbooks from an excel file by using the name of the workbook, when attempting to use the current approach the workbook does not match ds.Tables(x). For example the 5th workbook does not match ds.Tables(5)

 Try
          
            ' InitializeComponent()
            'MsgBox(username.Text)
            '  Dim filename As String = "\Data\MasterFiles\combineSheets.xls"
            Dim filename As String = Application.StartupPath & "\Data\MasterFilesExcel\MasterFiles" & username.Text & ".xls"

            'Dim filename As String = Application.StartupPath & "\Data\LinkFiles\aop29combinedaa.xls"
            Dim myConnection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; " & "data source='" & filename & "';" & "Extended Properties=""Excel 12.0;HDR=YES;IMEX=1"" ")
            myConnection.Open()
            Dim mySheets As DataTable = myConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"})
            ' Dim ds As New DataSet()
            'Dim dt As DataTable

            For i As Integer = 0 To mySheets.Rows.Count - 1
                dt = makeDataTableFromSheetName(filename, mySheets.Rows(i)("TABLE_NAME").ToString())
                ds.Tables.Add(dt)
            Next
            Master.Visible = True
            Master.C1TrueDBGrid1.DataSource = ds.Tables(1)
            Master.C1TrueDBGrid1.Splits(0).DisplayColumns(0).AutoSize()
            Master.C1TrueDBGrid1.Splits(0).DisplayColumns(1).AutoSize()
            Master.C1TrueDBGrid1.Splits(0).DisplayColumns(2).Visible = False
            '****************************************************************************
            Master.C1TrueDBGrid2.DataSource = ds.Tables(13)
            Master.C1TrueDBGrid2.Splits(0).HighLightRowStyle.BackColor = Drawing.Color.Blue
            Master.C1TrueDBGrid2.Splits(0).HighLightRowStyle.ForeColor = Drawing.Color.White

            'For Each row As DataRow In dtsetcol13.Tables(0).Rows
            'Master.C1TrueDBGrid2.Columns("C_ID").ValueItems.Values.Add(New C1.Win.C1TrueDBGrid.ValueItem(row("C_ID"), row("C_ID")))
            'Next

            ' Master.C1TrueDBGrid2.Columns("C_ID").ValueItems.Validate = True
            'Master.C1TrueDBGrid2.Columns("C_ID").ValueItems.Presentation = C1.Win.C1TrueDBGrid.PresentationEnum.SortedComboBox
            ' Master.C1TrueDBGrid2.Columns(0).Caption = "W_ID"
            'Master.C1TrueDBGrid2.Splits(0).DisplayColumns(0).AutoSize()
            Master.C1TrueDBGrid2.Splits(0).DisplayColumns(0).AutoSize()
            Master.C1TrueDBGrid2.Splits(0).DisplayColumns(1).AutoSize()
             Master.C1TrueDBGrid2.Splits(0).DisplayColumns(2).Visible = False
                     '****************************************************************************
            'Exit Sub
            Master.C1TrueDBGrid3.DataSource = ds.Tables(10)

            Master.C1TrueDBGrid4.DataSource = ds.Tables(12)
            Master.C1TrueDBGrid5.DataSource = Nothing

            Master.C1TrueDBGrid7.DataSource = ds.Tables(11)
            Master.C1TrueDBGrid8.DataSource = ds.Tables(6)
            Master.C1TrueDBGrid9.DataSource = ds.Tables(9)
            Master.C1TrueDBGrid10.DataSource = Nothing
            Master.C1TrueDBGrid11.DataSource = Nothing
            Master.C1TrueDBGrid6.DataSource = ds.Tables(0)

            Master.C1TrueDBGrid13.DataSource = ds.Tables(10)
            Master.C1TrueDBGrid14.DataSource = ds.Tables(6)
            Master.C1TrueDBGrid15.DataSource = ds.Tables(4)
            Master.C1TrueDBGrid16.DataSource = ds.Tables(5)
            Master.C1TrueDBGrid17.DataSource = ds.Tables(9)

            Master.C1TrueDBGrid18.DataSource = ds.Tables(13)
            Master.C1TrueDBGrid19.DataSource = ds.Tables(14)
            Master.C1TrueDBGrid20.DataSource = Nothing
            Master.C1TrueDBGrid21.DataSource = Nothing

           
        Catch

            MsgBox(Err.Description)
        End Try

Open in new window

0
Comment
Question by:vcharles
3 Comments
 
LVL 32

Accepted Solution

by:
Robberbaron (robr) earned 500 total points
Comment Utility
dont know what the makeDataTableFromSheetName does exactly , but may be good idea to set name of table as i dont believe you can be certain of the order.

and in any case , ds.Tables(5) would be the sixth , as it starts from zero.

            For i As Integer = 0 To mySheets.Rows.Count - 1
                dt = makeDataTableFromSheetName(filename, mySheets.Rows(i)("TABLE_NAME").ToString()).
                dt.Name = filename    '<<< check for valid characters ??
                ds.Tables.Add(dt)
            Next

now you can use ds.Tables("myName1") instead of ds.Tables(11)
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
"For example the 5th workbook does not match ds.Tables(5)"
Since the collection is 0-based, the 5th worksheet would be ds.Tables(4).
0
 

Author Closing Comment

by:vcharles
Comment Utility
Thank You!
0

Featured Post

What Security Threats Are You Missing?

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 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