|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
ENCODEURL 函数返回 URL 编码的字符串,将某些非字母数字字符替换为百分比符号 (%) 和十六进制数字。
这个论坛已经有各种参考代码 ,最简单的就是 WorksheetFunction.EncodeURL,但这个 需要excel版本兼容 还有字数也有限制。
用 htmlfile 32/64 通用
- Function encodeURIComponent(ByVal strText As String) As String '如果值中带有非英文和数字,则需转换成%形式
- Set oDom = CreateObject("htmlfile")
- encodeURIComponent = oDom.parentWindow.encodeURIComponent(strText)
- Set oDom = Nothing
- End Function
复制代码 encodeURIComponent 函数 会保留 '(英语单引号),还有 , - _ . ! ~ * ' ( ) 这些 符号,如果不想 保留这些字符 用这个函数
- Function rfc3986EncodeURIComponent(ByVal strText As String) As String '如果值中带有非英文和数字,则需转换成%形式
- Set oDom = CreateObject("htmlfile")
- oDom.parentWindow.execScript "function rfc3986EncodeURIComponent(str){return encodeURIComponent(str).replace(/[!'()*]/g, escape);};"
- rfc3986EncodeURIComponent = oDom.parentWindow.rfc3986EncodeURIComponent(strText)
- Set oDom = Nothing
- End Function
复制代码 参考:https://stackoverflow.com/questi ... -quotes-apostrophes
参考:https://club.excelhome.net/threa ... tml?_dsign=b93d9ebe
EncodeURI ADO 不会保留 '(英语单引号)
- Function EncodeURI(ss, Optional ByVal CharSet1 As String = "UTF-8") As String
- On Error GoTo err1
- Dim ADODB_Stream
- Set ADODB_Stream = CreateObject("ADODB.Stream")
- Dim encode() As Byte
- With ADODB_Stream
- .Charset = CharSet1
- .Open
- .WriteText ss, 0
- .Position = 0
- .Type = 1
- If CharSet1 <> "UTF-8" Then
- .Position = 0
- Else
- .Position = 3 ''无 bom
- End If
- encode = .Read()
- EncodeURI = ""
- For i = 0 To UBound(encode)
- EncodeURI = EncodeURI & "%" & Hex(encode(i))
- Next
- .Close
- End With
- err1:
- Set ADODB_Stream = Nothing
- End Function
复制代码
|
评分
-
2
查看全部评分
-
|