|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub test_csv()
- Dim strFile As String
- With Application.FileDialog(msoFileDialogOpen) '1
- .InitialFileName = ThisWorkbook.Path
- With .Filters
- .Clear
- .Add "CSV Files", "*.csv"
- End With
- .AllowMultiSelect = False
- If .Show Then strFile = .SelectedItems(1) Else Exit Sub
- End With
-
- Application.ScreenUpdating = False
-
- Dim Conn As Object, rs As Object, SQL As String
- Dim p As String, f As String, i As Integer
-
- f = Split(strFile, "\")(UBound(Split(strFile, "\")))
- p = Replace(strFile, f, vbNullString)
-
- Set Conn = CreateObject("ADODB.Connection")
- Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='text;HDR=yes;FMT=Delimited';Data Source=" & p
-
- SQL = "SELECT * FROM [" & f & "]"
- Set rs = Conn.Execute(SQL)
- With Worksheets(2)
- .UsedRange.Clear
- For i = 0 To rs.Fields.Count - 1
- .Range("A1").Offset(0, i) = rs.Fields(i).Name
- Next
- .Range("A2").CopyFromRecordset rs
- With .Range("A1").CurrentRegion
- .Borders.Weight = xlHairline
- .Columns(18).NumberFormatLocal = "hh:mm:ss"
- End With
- .Name = Split(f, ".csv")(0)
- End With
-
- Set rs = Nothing
- Conn.Close
- Set Conn = Nothing
-
- Application.ScreenUpdating = True
- Beep
- End Sub
复制代码 |
|