Dolen

导航

.net读取excel数据到DataSet中

Dim objExcelFile As Excel.Application
        Dim objWorkBook As Excel.Workbook
        Dim objSheet As Excel.Worksheet

        objExcelFile = New Excel.Application
        objExcelFile.DisplayAlerts = False
        objWorkBook = objExcelFile.Workbooks.Open(filePath)
        objSheet = objWorkBook.Sheets(1)

        Dim ds As DataSet
        Dim da As OleDbDataAdapter
        Dim ExcelConn As OleDbConnection
        Dim strLength As Integer
        Dim ext As String
        Dim strCon As String
        Dim strSql As String
        Dim sInfro As String = ""
        Dim path As String = ""
        Dim dtDATA As DataTable

        strLength = filePath.LastIndexOf(".")
        ext = filePath.Substring(strLength, filePath.Length - strLength)  '扩展名

        If ext = ".xlsx" Then
            strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + filePath + "';Extended Properties='Excel 12.0;HDR=False;IMEX=1'"
        Else
            strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + filePath + "';Extended Properties='Excel 8.0;HDR=False;IMEX=1'"
        End If
        ExcelConn = New OleDbConnection(strCon)
        ExcelConn.Open()
        dtDATA = ExcelConn.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, Nothing)
        strSql = String.Format("select * from [" & dtDATA.Rows(0)(2).ToString & "]")
        da = New OleDbDataAdapter(strSql, ExcelConn)
        ds = New DataSet()
        da.Fill(ds)
        ExcelConn.Close()

 

posted on 2018-09-13 10:39  Dolen  阅读(196)  评论(0编辑  收藏  举报