将单元格中的汉字和字母写入.CSV
Write Chinese Characters And Alphabet in Cells Into .CSV
我正在尝试将数据写入 CSV 文件。数据包含汉字和一般文本。将其导出到 CSV 文件后,我的结果如下所示:
但它应该是这样的:
所有的汉字,如“物料申请系统”,"ADC培训"等全部转为“?”。
这是我的代码:
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
我正在尝试将数据写入 CSV 文件。数据包含汉字和一般文本。将其导出到 CSV 文件后,我的结果如下所示:
但它应该是这样的:
所有的汉字,如“物料申请系统”,"ADC培训"等全部转为“?”。
这是我的代码:
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