我使用下面的代码。
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,
答案 0 :(得分:2)
您当前的问题与引号无关。仅当该字段包含逗号/分号,换行符和引号时,才需要在字段周围引用。如果它包含任何引号,那么这些引号必须显示为TWICE,例如包含He said "Hello"
的字段应显示为"He said ""Hello"""
。
您确实需要找出区域设置中的分隔符字符。要找出的一种方法是启动Excel,将一些数据输入到几个单元格中,然后将其另存为CSV格式。退出Excel并使用记事本查看该文件。
顺便说一句,你最后会加一个额外的逗号;这将使你的文件看起来像有11列,第11列是空的。
","
会比ChrW(44)
稍微更具可读性吗?