Solved

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

Posted on 2014-11-05
3
204 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
ID: 40426004
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
ID: 40426238
"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
ID: 40426257
Thank You!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 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

23 Experts available now in Live!

Get 1:1 Help Now