|
楼主 |
发表于 2024-9-14 23:07
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
已解决
' utf8无BOM头编码写入文件
Function WriteUtf8WithoutBom( _
ByVal f As String, ByVal st As String)
Set stream = CreateObject("ADODB.stream")
Set BStream = CreateObject("ADODB.stream")
stream.Open
stream.Type = 2
stream.Charset = "utf-8"
stream.WriteText st
stream.Position = 3
BStream.Type = 1
BStream.Mode = 3
BStream.Open
stream.CopyTo BStream
stream.Flush
stream.Close
BStream.SaveToFile f, 2
BStream.Flush
BStream.Close
End Function
Sub utf8WBom()
Dim stxt$, af$, bf$, fso As Object
af = ThisWorkbook.Path & "\1.txt" ‘或指定路径 af = SourcePath & "c:\1.txt"
bf = ThisWorkbook.Path & "\2.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set tf = fso.OpenTextFile(af, 1)
stxt = tf.readall '读取全部
Call WriteUtf8WithoutBom(bf, stxt)
Set tf = Nothing
Set fso = Nothing
End Sub |
|