Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Excel File Into DataSet
Message
From
25/02/2010 19:57:20
 
General information
Forum:
ASP.NET
Category:
ADO.NET
Miscellaneous
Thread ID:
01451119
Message ID:
01451122
Views:
31
>Like this:
>
>
>private static DataSet _GetExcelDS()
>{
>    string SourceFile = @"C:\Projects\InvTrack\Data\SourceData.xls";
>    DataAccess da = new DataAccess();
>    da.ProviderInvariantName = "System.Data.OleDb";
>    da.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + SourceFile + ";Extended Properties=Excel 8.0;";
>
>    DataSet dsRetVal = da.ExecuteQuery("select * from [invdata$]", CommandType.Text);
>
>    return dsRetVal;
>}
>
Can you try with this class and let me know. You would have to adjust it to fit your environment but within a few minutes, you'll be able to try it with a new approach:
Imports System.Data.OleDb

Namespace Framework

    Public Class Import

        Private oApp As Framework.App
        Private oConnection As OleDbConnection
        Private oProcess As Framework.LXProcess
        Public aMessage(2, 4) As Object
        Public cError As String = ""
        Public cSource As String = ""
        Public cWorkbook As String = "Sheet1"
        Public nCount As Integer = 0
        Public nLanguage As Integer = 1
        Public oDataAdapter As OleDbDataAdapter
        Public oDataSet As DataSet

        ' This is when we access the data provider in desktop and Web service mode
        Sub New(ByVal toApplication As Framework.App)
            oApp = toApplication
            nLanguage = oApp.nLanguage
            Init()
        End Sub

        ' This is when we access the data provider in a Web mode
        Public Sub New(ByVal toProcess As Framework.LXProcess)
            oProcess = toProcess
            oApp = oProcess.oApp
            nLanguage = oProcess.nLanguage
            Init()
        End Sub

        Private Sub Init()

            ' 1 The ##File## file has to be submitted

            ' Text in English
            aMessage(1, 1) = "The Excel sheet should be in a XLS format."
            aMessage(2, 1) = "A sheet called ""Schema"" is not found."

            ' Text in French
            aMessage(1, 2) = "Le fichier Excel doit être dans un format XLS."
            aMessage(2, 2) = "Une feuille appelée ""Schema"" est introuvable."

            ' Text in Spanish
            aMessage(1, 3) = "El fichero Excel debe estar en un tamaño XLS."
            aMessage(2, 3) = "Una hoja llamada ""Schema"" es dificil de encontra."

            ' Text in Portuguese
            aMessage(1, 4) = "The Excel sheet should be in a XLS format."
            aMessage(2, 4) = "A sheet called ""Schema"" is not found."

        End Sub

        ' 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)

            oDataSet = New DataSet

            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

    End Class

End Namespace
And, this for the client:
        Dim loImport As Framework.Framework.Import = New Framework.Framework.Import(oProcess)
        Dim loRow As DataRow

        ' Assign the Excel file
        loImport.cSource = cFile

        ' Assign the workbook
        loImport.cWorkbook = "Schema"

        ' Import into a dataset
        If Not loImport.ImportFromExcel() Then
            cHtml = loImport.cError
            Return False
        End If

        ' Import all the rows
        For lnCounter = 0 To loImport.nCount - 1
            loRow = loImport.oDataSet.Tables(0).Rows(lnCounter)
            ...put your code to handle each row here
        Next
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
Previous
Reply
Map
View

Click here to load this message in the networking platform