|
- Sub test1() '导入到 Excel
- Dim p As String, f As String, i As Integer
- p = ThisWorkbook.Path & "\"
- f = "test.csv"
- If Len(Dir(p & f)) = 0 Then MsgBox "!": Exit Sub
- Range("A1").CurrentRegion.ClearContents
- Dim Conn As Object, rs As Object, SQL As String
- Set Conn = CreateObject("ADODB.Connection")
- Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='text;HDR=yes;FMT=CSVDelimited';Data Source=" & p
- SQL = "SELECT * FROM [" & f & "]"
- Set rs = Conn.Execute(SQL)
- For i = 0 To rs.Fields.Count - 1
- Range("A1").Offset(0, i) = rs.Fields(i).Name
- Next
- Range("A2").CopyFromRecordset rs
- Set rs = Nothing
- Conn.Close
- Set Conn = Nothing
- Beep
- End Sub
- Sub test2() '导入到 Access
- Dim p As String, f As String
- p = ThisWorkbook.Path & "\"
- f = "test.csv"
- If Len(Dir(p & f)) = 0 Then MsgBox "!": Exit Sub
- Dim Conn As Object, SQL As String
- Set Conn = CreateObject("ADODB.Connection")
- Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & p & "test.accdb"
- SQL = "SELECT * FROM [Text;FMT=Delimited;HDR=Yes;Database=" & p & ";].[" & f & "]"
-
- SQL = "INSERT INTO 测试表 SELECT * FROM(" & SQL & ")" '已存在 测试表 用此句
- 'SQL = "SELECT * INTO 测试表 FROM(" & SQL & ")" '不存在 测试表 用此句
-
- Conn.Execute SQL
- Conn.Close
- Set Conn = Nothing
- Beep
- End Sub
复制代码 |
|