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

时间:2019-04-02 08:22:23

标签: excel vba export-to-csv utf cjk

我正在尝试将数据写入CSV文件。数据包含汉字和常规文本。将其导出到CSV文件后,结果如下:

enter image description here

但是应该看起来像这样:

enter image description here

所有汉字,例如“物料申请系统”,“ 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

2 个答案:

答案 0 :(得分:1)

感谢@Ron Rosenfeld建议使用 FileSystemObject 。我修改了以下代码后,便能够生成包含汉字和常规文本的CSV文件,而不会出现任何问题:

fgdbError   hr;
wstring     errorText;
Geodatabase geodatabase;
string message;

   if ((hr = OpenGeodatabase(L"D:/User/GEO/GlobalNationDB.gdb", geodatabase)) != S_OK)
{
    message = "An error occurred while opening the geodatabase";
    ErrorInfo::GetErrorDescription(hr, errorText);
}
message = "The geodatabase has been opened";

string Datasetdefs;
hr = geodatabase.GetDatasetDefinition(L"//", L"Feature Dataset", Datasetdefs);
if (S_OK != hr) {
    message = message + "; Error occured when trying to get childDatasets from root";
}
else {
    message = message +  "; Successfully get Child Datasets";
}

CloseGeodatabase(geodatabase);

答案 1 :(得分:0)

尝试类似的东西:

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

您还可以使用FileSystemObject

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