使用vba creat csv但csv的格式不正确
我使用如下代码。
Private Sub CommandButton1_Click()
Const File$ = "C:\CsvfileTest2.csv"
Dim Fso, MyFile
Set Fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = Fso.CreateTextFile(File, True)
MyFile.Close
With CreateObject("ADODB.Stream")
.Open
.LoadFromFile File
.Position = 0
.Charset = "UTF-8"
.SaveToFile File, 2
.Close
End With
'Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set MyFile = Fso.OpenTextFile(File, 8, True, True)
' Just for the example:
myrange1 = 10
box = ""
For i = 1 To myrange1
box = ""
For j = 1 To 10
box = box & Sheet1.Cells(i, j) & ChrW(44)
Next j
MyFile.WriteLine box
Next i
MyFile.Close
End Sub
我创建的文件不是真正的 csv。有人可以帮助我吗? 如果我双击 csv 文件,它会像这样出现。每条线出现在一个单元格中,而不是出现在十个不同的单元格中。
a、s、d、sdf、sdf、dsdd、dd、dsd、sd、sds、
a、s、d、sdf、sdf、dsdd、dd、dsd、sd、sds、
a、s、d、sdf、sdf、 dsdd,dd,dsd,sd,sds,
a,s,d,sdf,sdf,dsdd,dd,dsd,sd,sds,
a,s,d,sdf,sdf,dsdd,dd,dsd,sd,sds,
a、s、d、sdf、sdf、dsdd、dd、dsd、sd、sds、
a、s、d、sdf、sdf、dsdd、dd、dsd、sd、sds、
a、s、d、sdf、sdf、 dsdd,dd,dsd,sd,sds,
a,s,d,sdf,sdf,dsdd,dd,dsd,sd,sds,
a,s,d,sdf,sdf,dsdd,dd,dsd,sd,sds,
I use the code as below.
Private Sub CommandButton1_Click()
Const File$ = "C:\CsvfileTest2.csv"
Dim Fso, MyFile
Set Fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = Fso.CreateTextFile(File, True)
MyFile.Close
With CreateObject("ADODB.Stream")
.Open
.LoadFromFile File
.Position = 0
.Charset = "UTF-8"
.SaveToFile File, 2
.Close
End With
'Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set MyFile = Fso.OpenTextFile(File, 8, True, True)
' Just for the example:
myrange1 = 10
box = ""
For i = 1 To myrange1
box = ""
For j = 1 To 10
box = box & Sheet1.Cells(i, j) & ChrW(44)
Next j
MyFile.WriteLine box
Next i
MyFile.Close
End Sub
the file which I creat is not a realy csv. can anybody help me?
if I double click csv file, it will appeare like this. each line appear in one cell not in ten different cells.
a,s,d,sdf,sdf,dsdd,dd,dsd,sd,sds,
a,s,d,sdf,sdf,dsdd,dd,dsd,sd,sds,
a,s,d,sdf,sdf,dsdd,dd,dsd,sd,sds,
a,s,d,sdf,sdf,dsdd,dd,dsd,sd,sds,
a,s,d,sdf,sdf,dsdd,dd,dsd,sd,sds,
a,s,d,sdf,sdf,dsdd,dd,dsd,sd,sds,
a,s,d,sdf,sdf,dsdd,dd,dsd,sd,sds,
a,s,d,sdf,sdf,dsdd,dd,dsd,sd,sds,
a,s,d,sdf,sdf,dsdd,dd,dsd,sd,sds,
a,s,d,sdf,sdf,dsdd,dd,dsd,sd,sds,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您当前的问题与报价无关。仅当字段包含任何逗号/分号、换行符和引号时,才需要在该字段周围加上引号。如果它包含任何引号,这些引号必须出现两次,例如包含
He said "Hello"
的字段应显示为"He said ""Hello"""
。您确实需要找出您所在区域设置中的分隔符。一种查找方法是启动 Excel,在几个单元格中键入一些数据,然后将其另存为 CSV。退出 Excel 并使用记事本查看该文件。
顺便说一下,你在最后多加了一个逗号;这将使您的文件看起来有 11 列,第 11 列为空。
","
会比ChrW(44)
更具可读性吗?Your current problem is nothing to do with quotes. You need quotes around a field only when that field contains any commas/semicolons, line breaks, and quotes. If it contains any quotes, those quotes must appear TWICE e.g. field containing
He said "Hello"
should appear as"He said ""Hello"""
.You do need to find out what is the separator character in your locale. One way to find out is to start Excel, type some data into a few cells, then save it as CSV. Exit Excel and look at the file using Notepad.
By the way, you are putting out an extra comma at the end; that will make your file look like it has 11 columns, with the 11th one empty.
Would
","
be slightly more readable thanChrW(44)
?