Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How about those Excel column with a space at the end
Message
De
04/05/2011 21:27:54
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Titre:
How about those Excel column with a space at the end
Versions des environnements
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01509536
Message ID:
01509536
Vues:
107
If you import an Excel sheet into a dataset, if the Excel sheet has columns with a space at the end, those are preserved at the Excel level. So, basically, this creates a problem when referencing the dataset column as it will not exist. For example, I am using this to import an Excel sheet:
        ' Import from Excel
        Public Function ImportFromExcel() As Boolean
            Dim lcConnection As String = ""

            lcConnection = "provider=Microsoft.Jet.OLEDB.4.0; " + _
             "data source='" + cSource + "'; Extended Properties=Excel 8.0;"

            ' Assign the source directory
            oConnection = New OleDbConnection(lcConnection)

            ' Select the data from Sheet1 of the workbook
            oDataAdapter = New OleDbDataAdapter("Select * From [" + cWorkbook + "$]", oConnection)

            Try
                oDataAdapter.Fill(oDataSet)
            Catch loError As Exception
                cError = loError.Message

                ' Try to extrapolate
                Select Case cError

                    Case "External table is not in the expected format."
                        cError = aMessage(1, nLanguage)

                    Case "'Schema$' is not a valid name.  Make sure that it does not include invalid characters or punctuation and that it is not too long."
                        cError = aMessage(2, nLanguage)

                End Select

                Return False
            End Try

            ' Record count
            nCount = oDataSet.Tables(0).Rows.Count

            Return True
        End Function
This is pretty straight forward. Now, I can do this to access a row and a column:
        ' Import all the rows
        For lnCounter = 0 To loImport.nCount - 1
            loRow = loImport.oDataSet.Tables(0).Rows(lnCounter)

            Try

                If Not loRow("Numero") Is DBNull.Value Then
                    lnNumero = Val(loRow("Numero"))
                End If
So, at this point, I assume the column Numero to exist. But, in Excel, if the user named that column Numero + a space at the end, this is imported as is. Is there any way to know about it, trim it or avoid it?
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Répondre
Fil
Voir

Click here to load this message in the networking platform