VB 怎么更改TXT编码格式和读取Txt指定字符
发布网友
发布时间:2022-10-27 22:13
我来回答
共2个回答
热心网友
时间:2023-09-30 03:25
VB可以通过CreateObject函数来实现打开UTF-8编码格式文本文件,将它显示于文本框中。并可通过Print # 语句将格式化显示的数据以ANSI格式写入顺序文件中。
CreateObject函数,创建并返回一个对 ActiveX 对象的引用。
Print # 语句,将格式化显示的数据写入顺序文件中。
以下是实现代码:
Private Sub Command1_Click()
Dim ados As Object
Dim str As String
CommonDialog1.FileName = ""
CommonDialog1.Filter = "文本文件(*.txt)|*.txt|"
CommonDialog1.Action = 1
str = CommonDialog1.FileName
Debug.Print str
Set ados = CreateObject("adodb.stream")
With ados
.Charset = "utf-8"
.Type = 2
.Open
.LoadFromFile str
Text1.Text = .ReadText
.Close
End With
Debug.Print Mid(str, 1, Len(str) - 4) & "1.txt"
Open Mid(str, 1, Len(str) - 4) & "1.txt" For Output As #1
Print #1, Text1.Text
Close #1
End Sub
热心网友
时间:2023-09-30 03:26
这是读取utf8文本
Dim Mazmun As String
Dim strFileName As String
strFileName = "c:\aa.txt"
Dim a
Set a = CreateObject("ADODB.Stream")
a.Charset = "utf-8"
a.Open
a.LoadFromFile strFileName
Mazmun = a.ReadText
a.Close
MsgBox Mazmun
下面是更改编码:
Dim Mazmun As String
Dim strFileName As String
strFileName = "c:\aa.txt"
Dim a
Set a = CreateObject("ADODB.Stream")
a.Charset = "utf-8"
a.open
a.LoadFromFile strFileName
Mazmun = a.ReadText
a.Close
Dim stm
Set stm = CreateObject("adodb.stream")
stm.Type = 2
stm.mode = 3
stm.Charset = "gb2312"
stm.open
stm.WriteText Mazmun
stm.SaveToFile strFileName, 2
stm.flush
stm.Close
Set stm = Nothing
MsgBox "utf8转换了GBK"