|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 lzhqlj 于 2015-5-5 22:23 编辑
通过ADO 使用SQL将 当前目录下的csv快速转换为SQL代码如下Private Sub CommandButton1_Click()
Dim s, name As String
Dim r As Range
Dim lk As Long
Application.ScreenUpdating = False
p = ThisWorkbook.Path & "\"
s = ThisWorkbook.Path
s = Right(s, 8)
s = Mid(s, 1, 4) & "-" & Mid(s, 5, 2) & "-" & Mid(s, 7, 2)
Set Fso = CreateObject("Scripting.FileSystemObject")
Set cnn = CreateObject("adodb.connection")
cnn.Open "provider=Microsoft.Ace.OLEDB.12.0;Extended Properties =Excel 12.0;Data Source=" & ThisWorkbook.FullName
For Each File In Fso.GetFolder(ThisWorkbook.Path).Files
If File.name Like "*.csv" Then
f = Replace(File, ".csv", ".xlsx")
If Fso.FileExists(f) Then Fso.DeleteFile (f)
Sql = "SELECT * INTO [Excel 12.0 xml;Database=" & f & ";]." & Replace(File.name, ".csv", "") & " FROM [Text;FMT=Delimited;HDR=Yes;DATABASE=" & p & ";]." & File.name
'cnn.Execute Sql
Next
MsgBox "OK"
End Sub
-----------
CSV无列名格式如下
92501 | 8.03 | S | 500 | 92501 | 8.03 | S | 500 | 92501 | 8.03 | S | 500 | 92501 | 8.03 | S | 1400 | 92501 | 8.03 | S | 8000 | 92501 | 8.03 | S | 27600 | 92501 | 8.03 | S | 200 | 92501 | 8.03 | S | 900 | 92501 | 8.03 | S | 1600 | 92501 | 8.03 | B | 500 | 需要转换成xlsx格式如下
时间 | 价格 | 方向 | 数量 | 日期 | 代码 | 92501 | 8.03 | S | 500 | 2014/6/7 | 6001 | 92501 | 8.03 | S | 500 | 2014/6/7 | 6001 | 92501 | 8.03 | S | 500 | 2014/6/7 | 6001 | 92501 | 8.03 | S | 1400 | 2014/6/7 | 6001 | 92501 | 8.03 | S | 8000 | 2014/6/7 | 6001 | 92501 | 8.03 | S | 27600 | 2014/6/7 | 6001 | 92501 | 8.03 | S | 200 | 2014/6/7 | 6001 | 92501 | 8.03 | S | 900 | 2014/6/7 | 6001 | 92501 | 8.03 | S | 1600 | 2014/6/7 | 6001 | 92501 | 8.03 | B | 500 | 2014/6/7 | 6001 | -------------------------------------
现在可以将CSV转换成XLSX 但是转换的xlsx文件无列名 和后加的日期、代码列;现在使用
.Sheets(1).Cells(1, 1) = "成交时间"等实现 速度比较慢
尝试使用 Sql = " alter table [Excel 12.0 xml;Database=" & f & ";]." & Replace(File.name, ".csv", "") & " add File.name varchar(30)"
MsgBox Sql
cnn.Execute Sql
提示自动化错误,我觉的用sql语句可以实现,哪位大神能指导下小弟 谢谢
|
|