Leggere un file Microsoft Excel 97/2003 o 2007

by enrico 7. April 2009 19:32

La funzione che presento oggi consente di importare un foglio di Microsoft Excel in un dataset nelle versioni 97, 2003 e 2007.

 

   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

Buon lavoro e ricordatevi il nostro forum!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , ,

asp.net | vb.net | Tricks & Tips | Work around | windowsform

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by Esia Software 1.4.0.0

Informazioni sull'autore

Sono un cittadini di Trieste, ho 33 anni e faccio lo sviluppatore nella mia azienda che si chiama Esia Software.

Commenti recenti

Comment RSS

Post recenti

Calendar

<<  May 2012  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar

Info legali

Le opinioni espresse in questo blog sono strettamente personali e ogni persona è responsabile dei commenti che inserisce.

RecentComments

Comment RSS