VB 怎么更改TXT编码格式和读取Txt指定字符
现要读取一个txt文本里面的多条字符串记录,txt原本的编码格式为UTF-8,VB读取此格式下的中文字符成乱码,只能将txt的编码格式变为ANSI格式,请问VB中用代码怎...
现要读取一个txt文本里面的多条字符串记录,txt原本的编码格式为UTF-8,VB读取此格式下的中文字符成乱码,只能将txt的编码格式变为ANSI格式,请问VB中用代码怎么实现TXT编码格式的改变,还有怎么实现截取记录中指定字节数的的字符
展开
推荐于2016-03-03 · 知道合伙人软件行家
关注
展开全部
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
展开全部
这是读取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"
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"
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询