2009/07/07

ADO:CSV読み込み

ADOを使用した、CSV→recordset
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strFolder As String, cnStr As String
Dim fileName As String, selectSQL As String

cnStr = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq="
strFolder = "C:\hogehoge"
fileName = "hogehoge.csv"
selectSQL = "SELECT * FROM " & fileName

cn.Open cnStr & strFolder
rs.Open selectSQL, cn, adOpenKeyset, adLockReadOnly

Do Until rs.EOF
  **お好みのupdate処理**
rs.MoveNext
Loop

rs.Close: cn.Close
Set rs = Nothing: Set cn = Nothing

ヘッダがないなら、Jetです。
cnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
strFolder = "C:\hogehoge;"
exProp = "Extended Properties=""text;HDR=No;FMT=Delimited;"";"
cn.Open cnStr & strFolder & exProp

0 件のコメント: