|
楼主 |
发表于 2012-12-11 11:37
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
经测试不管哪种方法,都会:1、把长数字变为科学计数法,2、把数字文本导前0自动删除,如00123变成123
可用Open语句打开文本文件,再采用第1、2种方法导入到数据库:- Sub Open语句打开文本文件再用第一种方法() '引用Microsoft ActiveX Data Objects 2.x Library
- Dim cnnData As New ADODB.Connection
- Dim rsData As New ADODB.Recordset
- Dim myPath As String
- Dim MyFile As String
- Dim myTable As String
- Dim arr, s$(), i As Long, j As Long
- myTable = "贷款数据"
- cnnData.Open "provider=microsoft.jet.oledb.4.0;data source=" & ThisWorkbook.Path & "" & "db1.mdb"
- rsData.Open myTable, cnnData, adOpenKeyset, adLockOptimistic
- myPath = ThisWorkbook.Path & ""
- MyFile = Dir(myPath & "*.csv")
- With rsData
- Do While MyFile <> ""
- Open myPath & MyFile For Input As #1
- s = Split(StrConv(InputB(LOF(1), 1), vbUnicode), vbCrLf)
- Close #1
- For i = 1 To UBound(s) '忽略标题行
- If Len(s(i)) Then
- a = Split(s(i), ",")
- .AddNew
- For j = 0 To UBound(a)
- .Fields(j) = a(j)
- Next
- .Update
- End If
- Next
- MyFile = Dir()
- Loop
- End With
- MsgBox "已经成功将文本文件数据保存为数据库!", vbInformation
- rsData.Close
- cnnData.Close
- Set rsData = Nothing
- Set cnnData = Nothing
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|