本文介绍了VBA:使用没有 BOM 的 UTF-8 保存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这可能很简单,这是我尝试过的:
Set objStream = CreateObject("ADODB.Stream")设置 objStreamNoBOM = CreateObject("ADODB.Stream")使用 objStream.打开.Charset = "UTF-8".WriteText "aaaaaa".位置 = 0结束于使用 objStreamNoBOM'.Charset = "Windows-1252" ' 工作.Charset = "UTF-8" ' 不工作!!.打开. 类型 = 2.WriteText objStream.ReadText.SaveToFile "toto.php", 2.关闭结束于objStream.Close
如果字符集是 UTF-8,则文件开头有 ï».
知道如何使用 UTF-8 和不使用 BOM 保存文件吗?
解决方案
In the best of all possible worlds 相关列表将包含对 这个问题 我发现这是vbscript adodb.stream bom vbscript site:stackoverflow.com"的第一个命中.>
基于 boost 的答案的第二种策略:
选项显式常量 adSaveCreateNotExist = 1常量 adSaveCreateOverWrite = 2常量 adTypeBinary = 1常量 adTypeText = 2Dim objStreamUTF8 :设置 objStreamUTF8 = CreateObject("ADODB.Stream")Dim objStreamUTF8NoBOM :设置 objStreamUTF8NoBOM = CreateObject("ADODB.Stream")使用 objStreamUTF8.Charset = "UTF-8".打开.WriteText "aÄö".位置 = 0.SaveToFile "toto.php", adSaveCreateOverWrite.Type = adTypeText.位置 = 3结束于使用 objStreamUTF8NoBOM.Type = adTypeBinary.打开objStreamUTF8.CopyTo objStreamUTF8NoBOM.SaveToFile "toto-nobom.php", adSaveCreateOverWrite结束于objStreamUTF8.CloseobjStreamUTF8NoBOM.Close
证据:
chcp活动代码页:65001目录...15.07.2015 18:48 5 toto-nobom.php15.07.2015 18:48 8 toto.php输入 toto-nobom.php啊
it's probably sthg simple, here is what I tried :
Set objStream = CreateObject("ADODB.Stream")
Set objStreamNoBOM = CreateObject("ADODB.Stream")
With objStream
.Open
.Charset = "UTF-8"
.WriteText "aaaaaa"
.Position = 0
End With
With objStreamNoBOM
'.Charset = "Windows-1252" ' WORK
.Charset = "UTF-8" ' DOESN'T WORK!!
.Open
.Type = 2
.WriteText objStream.ReadText
.SaveToFile "toto.php", 2
.Close
End With
objStream.Close
if the charset is UTF-8, then there is ï» at the beginning of the file.
Any idea on how to save a file with UTF-8 and without BOM?
解决方案
In the best of all possible worlds the Related list would contain a reference to this question which I found as the first hit for "vbscript adodb.stream bom vbscript site:stackoverflow.com".
Based on the second strategy from boost's answer:
Option Explicit
Const adSaveCreateNotExist = 1
Const adSaveCreateOverWrite = 2
Const adTypeBinary = 1
Const adTypeText = 2
Dim objStreamUTF8 : Set objStreamUTF8 = CreateObject("ADODB.Stream")
Dim objStreamUTF8NoBOM : Set objStreamUTF8NoBOM = CreateObject("ADODB.Stream")
With objStreamUTF8
.Charset = "UTF-8"
.Open
.WriteText "aÄö"
.Position = 0
.SaveToFile "toto.php", adSaveCreateOverWrite
.Type = adTypeText
.Position = 3
End With
With objStreamUTF8NoBOM
.Type = adTypeBinary
.Open
objStreamUTF8.CopyTo objStreamUTF8NoBOM
.SaveToFile "toto-nobom.php", adSaveCreateOverWrite
End With
objStreamUTF8.Close
objStreamUTF8NoBOM.Close
Evidence:
chcp
Active code page: 65001
dir
...
15.07.2015 18:48 5 toto-nobom.php
15.07.2015 18:48 8 toto.php
type toto-nobom.php
aÄö
这篇关于VBA:使用没有 BOM 的 UTF-8 保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!