vb.net 讀取 excel


    Dim myConn AsNew ADODB.Connection

    myConn.CursorLocation = ADODB.CursorLocationEnum.adUseClient

        

    '   用於連接excel文件的字符串,注意: HDR=No,表示從第0行開始讀; HDR=Yes,表示從第1行開始讀

    Dim connStr = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & myEXCELFileName & ";" &                  

                            "Extended Properties='Excel 12.0 Xml; HDR=No; IMEX=1'"

    myConn.Open(connStr)     

 

    '  把excel作為數據源

    Dim rs AsNew ADODB.Recordset

    

     Try    '   打開Excel文件中的某張表 table0

             rs.Open( "Select * From [table0$]", myConn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)

     Catch ex AsException

          messagebox.show("Pls use excel file with the right sheet name - table0.")            

          Return False

     End  Try

        

     Dim i AsInteger

     Dim j AsInteger

     

      Dim dt_excel AsNewDataTable

      

       For i = 0 To rs.Fields.Count - 1        '   dataTable 添加列

            dt_excel.Columns.Add(NewDataColumn(rs.Fields(i).Name, GetType(String)))

       Next

       

        Dim da AsNew System.Data.OleDb.OleDbDataAdapter()

        da.Fill(dt_excel, rs)         '  把 讀取的記錄先放到 dataTable 中

 

        rs.Close()

        myConn.Close()

        rs =Nothing

        da =Nothing

        myConn =Nothing

        

       '  下面對 dataTable  進行操作

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM