如何从Excel导出的* .txt文件的编码从UTF-8更改为UTF-8-BOM? [英] How to change encoding from UTF-8 to UTF-8-BOM of exported *.txt files from Excel?

查看:83
本文介绍了如何从Excel导出的* .txt文件的编码从UTF-8更改为UTF-8-BOM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Excel导出的文本文件使用UTF-8编码.

Exported text files from Excel are encoded with UTF-8.

需要一个编码UTF-8-BOM.

An encoding UTF-8-BOM is needed.

我认为应在代码中插入一行,写成:

I think that in code shall be inserted a row, written like:

Java

?xml版本="1.0"编码="UTF-8"?

?xml version="1.0" encoding="UTF-8"?

没有BOM而不是BOM的Jasperreport CSV UTF-8UTF-8

HTML5

meta charset ="utf-8"

meta charset="utf-8"

没有BOM编码的错误UTF-8

Sub export_data()
Dim row, column, i, j As Integer
Dim fullPath, myFile As String

fullPath = "C:\Workspace"
row = 21
column = 5

For i = 1 To column
    myFile = Cells(1, i).Value + ".txt"
    myFile = fullPath + "/" + myFile
    Open myFile For Output As #1
    For j = 2 To row
        Print #1, Cells(j, i).Value
    Next j
    Close #1
Next i

End Sub

如何定义以及在哪里放置定义编码UTF-8-BOM的行?谢谢.

How can I define and where to put a row, which defines encoding UTF-8-BOM? Thank You.

推荐答案

与其逐行打印文件相比,

  • 将您选择的范围另存为 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.

    这篇关于如何从Excel导出的* .txt文件的编码从UTF-8更改为UTF-8-BOM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆