url解码函数(乱码编译成中文)
方法一:
Function URLDecode(ByVal strIn)
URLDecode = ""
Dim sl: sl = 1
Dim tl: tl = 1
Dim key: key = "%"
Dim kl: kl = Len(key)
sl = InStr(sl, strIn, key, 1)
Do While sl > 0
If (tl = 1 And sl <> 1) Or tl < sl Then
URLDecode = URLDecode & Mid(strIn, tl, sl - tl)
End If
Dim hh, hi, hl
Dim a
Select Case UCase(Mid(strIn, sl + kl, 1))
Case "U" 'Unicode URLEncode
a = Mid(strIn, sl + kl + 1, 4)
URLDecode = URLDecode & ChrW("&H" & a)
sl = sl + 6
Case "E" 'UTF-8 URLEncode
hh = Mid(strIn, sl + kl, 2)
a = Int("&H" & hh) 'ascii码
If Abs(a) < 128 Then
sl = sl + 3
URLDecode = URLDecode & Chr(a)
Else
hi = Mid(strIn, sl + 3 + kl, 2)
hl = Mid(strIn, sl + 6 + kl, 2)
a = ("&H" & hh And &HF) * 2 ^ 12 Or ("&H" & hi And &H3F) * 2 ^ 6 Or ("&H" & hl And &H3F)
If a < 0 Then a = a + 65536
URLDecode = URLDecode & ChrW(a)
sl = sl + 9
End If
Case Else 'Asc URLEncode
hh = Mid(strIn, sl + kl, 2) '高位
a = Int("&H" & hh) 'ascii码
If Abs(a) < 128 Then
sl = sl + 3
Else
hi = Mid(strIn, sl + 3 + kl, 2) '低位
a = Int("&H" & hh & hi) '非ascii码
sl = sl + 6
End If
URLDecode = URLDecode & Chr(a)
End Select
tl = sl
sl = InStr(sl, strIn, key, 1)
Loop
URLDecode = URLDecode & Mid(strIn, tl)
End Function
方法二
Public Function UrlDecode(ByVal strText As String) As String '如果值中带有非英文和数字,则需转换成%形式
'url编码 utf-8
Dim js
Set js = CreateObject("msscriptcontrol.scriptcontrol")
js.Language = "JavaScript"
'UrlDecode = js.eval("decodeURI('" & strText & "');") '忽略! @ # $& * ( ) = : / ; + '
'UrlEncode = js.Eval("escape('" & Replace(strText, "'", "\'") & "');") '汉字转换为%u开头的Unicode码 不会被此方法编码的字符: @ * / +
UrlDecode = js.Eval("decodeURIComponent('" & strText & "');") '包含://
End Function
如何使用excel自定义函数
1.打开excel
2.ALT+F11,会打开VB窗口
3.找到“插入”,选择插入模块
4.把以上代码分别复制到不同模块,然后保存。
5.关掉VB窗口,直接在顶部的输入框输入函数名字即可
如: