1: ''' <summary>
2: ''' Reads the data from excel.
3: ''' </summary>
4: ''' <param name="excelfilename">The excelfilename.</param>
5: ''' <returns></returns>
6: Function ReadDataFromExcel(ByVal excelfilename As String) As DataSet
7: Dim ds As New DataSet
8: Dim da As OleDbDataAdapter
9: Dim conn As OleDbConnection
10:
11: Try
12: If System.IO.Path.GetExtension(excelfilename) = ".xls" Then
13: conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & excelfilename & "; Extended Properties=Excel 8.0;")
14: ElseIf System.IO.Path.GetExtension(excelfilename) = ".xlsx" Then
15: conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & excelfilename & "; Extended Properties=Excel 8.0;")
16: End If
17:
18: da = New OleDbDataAdapter("SELECT * FROM [Foglio1$]", conn)
19:
20: conn.Open()
21: da.Fill(ds)
22: Catch ex As Exception
23: MsgBox(ex.Message)
24: Finally
25: If conn.State = ConnectionState.Open Then
26: conn.Close()
27: End If
28: End Try
29: ReadDataFromExcel = ds
30: End Function