Solved

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

Posted on 2014-11-05
3
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

737 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