-
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 进行操作
出处:https://www.cnblogs.com/gaoleionline/p/11730402.html