将单元格中的汉字和字母写入.CSV

问题描述 投票:0回答:2

我正在尝试将数据写入CSV文件。数据包含中文字符和一般文本。将结果导出为CSV文件后,我的结果如下所示:

enter image description here

但它应该看起来像这样:

enter image description here

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
excel vba export-to-csv utf cjk
2个回答
0
投票

尝试类似的东西:

 your_worksheet.SaveAs "your file path and name", xlUnicodeText

你也可以使用FileSystemObject

我不相信VBA Open for Output : Print (or Write)支持Unicode。


1
投票

感谢@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
© www.soinside.com 2019 - 2024. All rights reserved.