我正在尝试将数据写入CSV文件。数据包含中文字符和一般文本。将结果导出为CSV文件后,我的结果如下所示:
但它应该看起来像这样:
All the Chinese Characters, like "物料申请系统", "ADC培训", etc. are all turned into "?".
这是我的代码:
Open Location For Output As #1
For i = 2 To lastrow
For j = 1 To LastCol
If j = LastCol Then 'keep writing to same line
TextLine = TextLine & Cells(i, j).Text 'read line into variable
Else 'end the line
TextLine = TextLine & Cells(i, j).Text & Deliminator
End If
Next j
Print #1, TextLine
TextLine = ""
Next i
Close #1
尝试类似的东西:
your_worksheet.SaveAs "your file path and name", xlUnicodeText
你也可以使用FileSystemObject
我不相信VBA Open for Output : Print (or Write)
支持Unicode。
感谢@Ron Rosenfeld建议使用FileSystemObject。我能够使用中文字符和一般文本生成CSV文件,而我修改的代码没有任何问题:
With CreateObject("Scripting.FileSystemObject")
With .CreateTextFile(Location, , True)
For i = 2 To lastrow
For j = 1 To LastCol
If j = LastCol Then
TextLine = TextLine & Cells(i, j).Text
Else
TextLine = TextLine & Cells(i, j).Text & Deliminator
End If
Next j
.WriteLine TextLine
TextLine = ""
Next i
.Close
End With
End With