Can't get ExcelDataReader going - please help

Dec 6, 2009 at 1:56 PM

Sorry for being a Noob but...

 

I would really like to use this DLL to read data from Excel in VB.Net 2008.

 

I have copied the DLLs to the project directory and added a reference to them in the Project properties.And trying to follow the VB examples on this site.

 

(What is the difference between the ones in the main folder in the ZIP file and the ones in the CF folder?)

 

I get these errors on compile

 

Error    1    Type 'IExcelDataReader' is not defined.    C:\Users\john\Documents\Visual Studio 2008\Projects\ReadExcel\ReadExcel\Form1.vb    34    28    ReadExcel
Error    2    Name 'Factory' is not declared.    C:\Users\john\Documents\Visual Studio 2008\Projects\ReadExcel\ReadExcel\Form1.vb    37    27    ReadExcel
Error    3    Name 'ExcelFileType' is not declared.    C:\Users\john\Documents\Visual Studio 2008\Projects\ReadExcel\ReadExcel\Form1.vb    37    56    ReadExcel
Error    4    Name 'Factory' is not declared.    C:\Users\john\Documents\Visual Studio 2008\Projects\ReadExcel\ReadExcel\Form1.vb    39    27    ReadExcel
Error    5    Name 'ExcelFileType' is not declared.    C:\Users\john\Documents\Visual Studio 2008\Projects\ReadExcel\ReadExcel\Form1.vb    39    56    ReadExcel
Error    6    Local variable 'ds' is already declared in the current block.    C:\Users\john\Documents\Visual Studio 2008\Projects\ReadExcel\ReadExcel\Form1.vb    68    13    ReadExcel

 

Code

 

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        'Load spreadsheet file from specified format.

        Dim stream As FileStream = File.Open(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)

        Dim excelreader As IExcelDataReader

        If Microsoft.VisualBasic.Right(OpenFileDialog1.FileName, 5) = ".xlsx" Then
            excelreader = Factory.CreateReader(stream, ExcelFileType.OpenXml)
        Else
            excelreader = Factory.CreateReader(stream, ExcelFileType.Binary)
        End If

        Dim ds As DataSet = excelreader.AsDataSet

        'Reads only two worksheets

        'Do While excelreader.NextResult

        ' Do While excelreader.Read

        ' Dim res As String = String.Empty

        ' Dim objs() As Object

        ' Dim objcount As Int32 = excelreader.GetValues(objs)

        ' For i As Int32 = 0 To objcount - 1

        ' res &= String.Format("{0}, ", objs(i))

        ' Next

        ' Debug.Print(res)

        ' Loop

        'Loop

        Dim ds As DataSet = excelreader.AsDataSet

        For Each tbl As System.Data.DataTable In ds.Tables

            For Each row As DataRow In tbl.Rows

                Dim res As String = String.Empty

                For i As Int32 = 0 To row.ItemArray.Length - 1

                    res &= String.Format("{0}, ", row.ItemArray(i))

                Next

                Debug.Print(res)

            Next

        Next


    End Sub

 

Any idea what I am doing wrong.

 

Thanks in advance. John