|
本帖最后由 Benjamin_luk 于 2014-2-13 09:56 编辑
两个办法:
1. 自己写一个Schema.ini, 放在CSV目录下, 为每个CSV文件写下如下内容:
[009876N.csv]
Format=Delimited(|)
ColNameHeader=True
MaxScanRows=0
CharacterSet=ANSI
2.在读取CSV时, 将字符串用SPLIT(txt,"|")的方法将字符串转成单个值, 再写入数据表.
Option Compare Database
Public conn As New ADODB.Connection 'ADO
Public rs As New ADODB.Recordset
Public rs1 As New ADODB.Recordset
Public Function ReadCSVFile(ByVal strFilePath As String, ByVal strFileName As String)
Dim i As Integer, a
'===============================================================================
'-函数名称: ReadCSVFile
'-功能描述: 导入csv文件
'-输入参数说明: 参数1:strFilePath As String 目标路径
' 参数2:strFileName As String 文件名称
'-使用语法示例: ReadCSVFile("路径","文件名")
'-参考:
'-使用注意:
'-兼容性: 2000,XP,2003
'-作者: Grant
'-联系方式: QQ:20991943 Email:20991943@qq.com
'-更新日期: 2007-09-3
'===============================================================================
conn.Open "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
"DBQ=" & strFilePath & ";Extensions=asc,csv,tab,txtersist Security Info=False" '打开数据链接关键
With rs
.CursorType = 2
.LockType = 3
.CursorLocation = 3
.ActiveConnection = conn
End With
Set conn = CurrentProject.Connection
sqlcsv = "SELECT * FROM [" & strFileName & "]"
rs.Open sqlcsv
rs.Move (0) '移动到指定记录
sql = "Select * From tbl_Export"
rs1.Open sql, conn, 1, 3
Do Until rs.EOF
a = Split(rs(0), "|")
rs1.AddNew
For i = 0 To 7
rs1(i + 1) = a(i)
Next
rs.MoveNext
Loop
rs1.UpdateBatch
rs1.Close
rs.Close
conn.Close
Set rs = Nothing
Set rs1 = Nothing
Set conn = Nothing
End Function |
|