且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从Excel导出的* .txt文件的编码从UTF-8更改为UTF-8-BOM?

更新时间:2023-11-27 20:47:52

与其逐行打印文件相比,

  • 将您选择的范围另存为 CSV UTF-8
    • 保存后,您可能需要更改文件类型

    任何一种都会自动添加BOM.

    Either will add a BOM automatically.

    编辑

    如果您不熟悉,可以在打开宏录制器的情况下手动执行保存到csv-utf8 过程.然后检查您记录的内容并进行适当的编辑.

    If you are unfamiliar, you could perform the save to csv - utf8 process manually with the macro recorder turned on. Then examine what you have recorded and make appropriate edits.

    在现有代码的上下文中添加BOM的另一种方法是将其作为字节数组直接写入第一行.

    Another way of adding the BOM, in the context of your existing code, would be to write it directly as a byte array to the first line.

    例如:

    Dim BOM(0 To 2) As Byte 'EF BB BF
        BOM(0) = &HEF
        BOM(1) = &HBB
        BOM(2) = &HBF
    
    Open myFile For Binary Access Write As #1
        Put #1, 1, BOM
    Close #1
    

    会将BOM表放在文件的开头.然后,您应该在后续的 Print 代码中将模式更改为 Append .

    will put the BOM at the beginning of the file. You should then change the mode in your subsequent Print code to Append.

    我建议您阅读有关使用 Print Write

    I suggest you read about the pros and cons of using Print vs Write

    您还应该阅读有关声明语句的信息.在您的变量中,只有每行的最后一个变量被声明为指定的类型;前面的变量被隐式声明为Variant类型.

    You should also read about declaration statements. In yours, only the last variable on each line is being declared as the specified type; the preceding variables are being implicitly declared as being of type Variant.