重金悬赏!求VB代码:将下面的网页的文本读取到TextBox控件上。
http://m.weather.com.cn/data/101140602.html【请自己用下载器将这个文件下载下来,一定要是原封不动的文件!】本人找了近半夜未果,只...
http://m.weather.com.cn/data/101140602.html
【请自己用下载器将这个文件下载下来,一定要是原封不动的文件!】
本人找了近半夜未果,只知道是Text编码的问题……
【注:本人将文件名[101140602.html]私自改成了[文本.txt]】
【另】为了使后来者遇到此类问题能在这里得到解决,请尽量将代码、代码原理等写清楚。
此问题适合用VB读取某些文本文件出现乱码的情况——星空无月留 展开
【请自己用下载器将这个文件下载下来,一定要是原封不动的文件!】
本人找了近半夜未果,只知道是Text编码的问题……
【注:本人将文件名[101140602.html]私自改成了[文本.txt]】
【另】为了使后来者遇到此类问题能在这里得到解决,请尽量将代码、代码原理等写清楚。
此问题适合用VB读取某些文本文件出现乱码的情况——星空无月留 展开
4个回答
展开全部
utf-8转unicode即可。
Private Sub Command1_Click()
Open "c:\101140602.txt" For Binary As #1
Text1 = Utf8ToUnicode(InputB(LOF(1), 1))
End Sub
Private Function Utf8ToUnicode(ByRef Utf() As Byte) As String
Dim utfLen As Long
utfLen = -1
On Error Resume Next
utfLen = UBound(Utf)
If utfLen = -1 Then Exit Function
On Error GoTo 0
Dim i As Long, J As Long, k As Long, N As Long
Dim B As Byte, cnt As Byte
Dim Buf() As String
ReDim Buf(utfLen)
i = 0
J = 0
Do While i <= utfLen
B = Utf(i)
If (B And &HFC) = &HFC Then
cnt = 6
ElseIf (B And &HF8) = &HF8 Then
cnt = 5
ElseIf (B And &HF0) = &HF0 Then
cnt = 4
ElseIf (B And &HE0) = &HE0 Then
cnt = 3
ElseIf (B And &HC0) = &HC0 Then
cnt = 2
Else
cnt = 1
End If
If i + cnt - 1 > utfLen Then
Buf(J) = "?"
Exit Do
End If
Select Case cnt
Case 2
N = B And &H1F
Case 3
N = B And &HF
Case 4
N = B And &H7
Case 5
N = B And &H3
Case 6
N = B And &H1
Case Else
Buf(J) = Chr(B)
GoTo Continued:
End Select
For k = 1 To cnt - 1
B = Utf(i + k)
N = N * &H40 + (B And &H3F)
Next
Buf(J) = ChrW(N)
Continued:
i = i + cnt
J = J + 1
Loop
Utf8ToUnicode = Join(Buf, "")
End Function
Private Sub Command1_Click()
Open "c:\101140602.txt" For Binary As #1
Text1 = Utf8ToUnicode(InputB(LOF(1), 1))
End Sub
Private Function Utf8ToUnicode(ByRef Utf() As Byte) As String
Dim utfLen As Long
utfLen = -1
On Error Resume Next
utfLen = UBound(Utf)
If utfLen = -1 Then Exit Function
On Error GoTo 0
Dim i As Long, J As Long, k As Long, N As Long
Dim B As Byte, cnt As Byte
Dim Buf() As String
ReDim Buf(utfLen)
i = 0
J = 0
Do While i <= utfLen
B = Utf(i)
If (B And &HFC) = &HFC Then
cnt = 6
ElseIf (B And &HF8) = &HF8 Then
cnt = 5
ElseIf (B And &HF0) = &HF0 Then
cnt = 4
ElseIf (B And &HE0) = &HE0 Then
cnt = 3
ElseIf (B And &HC0) = &HC0 Then
cnt = 2
Else
cnt = 1
End If
If i + cnt - 1 > utfLen Then
Buf(J) = "?"
Exit Do
End If
Select Case cnt
Case 2
N = B And &H1F
Case 3
N = B And &HF
Case 4
N = B And &H7
Case 5
N = B And &H3
Case 6
N = B And &H1
Case Else
Buf(J) = Chr(B)
GoTo Continued:
End Select
For k = 1 To cnt - 1
B = Utf(i + k)
N = N * &H40 + (B And &H3F)
Next
Buf(J) = ChrW(N)
Continued:
i = i + cnt
J = J + 1
Loop
Utf8ToUnicode = Join(Buf, "")
End Function
追问
测试成功!请问能简述原理吗?
展开全部
'Ctrl+T里面勾选Microsoft Internet Control 6.0,并在窗体上画一个,默认名称是WebBrowser1
Name "C:\Users\Adminstrator\Desktop\文本.txt" AS "C:\Users\Adminstrator\Desktop\文本.html"
WebBrowser1.Navigate "C:\Users\Adminstrator\Desktop\文本.html"
Text1.text=WebBrowser1.Document.body.innerText
Name "C:\Users\Adminstrator\Desktop\文本.txt" AS "C:\Users\Adminstrator\Desktop\文本.html"
WebBrowser1.Navigate "C:\Users\Adminstrator\Desktop\文本.html"
Text1.text=WebBrowser1.Document.body.innerText
更多追问追答
追问
这个我当然会;只是如果用不常用的控件会使程序在移植时将控件带着,这并不符合我的本意
追答
我已经知道方法了,不超过五行代码。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不要用textbox,要用file list box 应该叫rtftext吧
追问
如果用不常用的控件会使程序在移植时将控件带着,这并不符合我的本意
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
只读汉字还是所有的都读出来?
更多追问追答
追问
只要能别读出乱码就行了
追答
不用其他控件?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询