我使用下面的代碼。使用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,自衛隊,日本自衛隊,dsdd,DD,渠務署,SD,SDS,
A,S,d,自衛隊,日本自衛隊,dsdd,DD,渠務署,SD,SDS,
我把它用於utf-8格式 – dadario 2010-07-27 05:49:04
在什麼意義上說「不是真正的csv」,即你的輸出有什麼問題? – Murph 2010-07-27 07:17:56
這可能是造成混淆的原因:Excel使用分號';'而不是逗號作爲處理CSV文件的分隔符。 – marg 2010-07-27 07:44:44