ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 555|回复: 2

[分享] URLEncode EncodeURL EncodeURI 自定义函数 32\64通用

[复制链接]

TA的精华主题

TA的得分主题

发表于 2023-6-3 10:56 | 显示全部楼层 |阅读模式
       ENCODEURL 函数返回 URL 编码的字符串,将某些非字母数字字符替换为百分比符号 (%) 和十六进制数字。

       这个论坛已经有各种参考代码 ,最简单的就是 WorksheetFunction.EncodeURL,但这个 需要excel版本兼容 还有字数也有限制。
       用 htmlfile 32/64 通用
  1. Function encodeURIComponent(ByVal strText As String) As String     '如果值中带有非英文和数字,则需转换成%形式
  2.     Set oDom = CreateObject("htmlfile")
  3.     encodeURIComponent = oDom.parentWindow.encodeURIComponent(strText)
  4.     Set oDom = Nothing
  5. End Function
复制代码
     encodeURIComponent 函数 会保留 '(英语单引号),还有 , - _ . ! ~ * ' ( ) 这些 符号,如果不想 保留这些字符 用这个函数
  1. Function rfc3986EncodeURIComponent(ByVal strText As String) As String      '如果值中带有非英文和数字,则需转换成%形式
  2.     Set oDom = CreateObject("htmlfile")
  3.     oDom.parentWindow.execScript "function rfc3986EncodeURIComponent(str){return encodeURIComponent(str).replace(/[!'()*]/g, escape);};"
  4.     rfc3986EncodeURIComponent = oDom.parentWindow.rfc3986EncodeURIComponent(strText)
  5.     Set oDom = Nothing
  6. End Function
复制代码
参考:https://stackoverflow.com/questi ... -quotes-apostrophes
参考:https://club.excelhome.net/threa ... tml?_dsign=b93d9ebe

EncodeURI ADO  不会保留 '(英语单引号)
  1. Function EncodeURI(ss, Optional ByVal CharSet1 As String = "UTF-8") As String
  2.     On Error GoTo err1
  3.     Dim ADODB_Stream
  4.     Set ADODB_Stream = CreateObject("ADODB.Stream")
  5.     Dim encode() As Byte
  6.     With ADODB_Stream
  7.         .Charset = CharSet1
  8.         .Open
  9.         .WriteText ss, 0
  10.         .Position = 0
  11.         .Type = 1
  12.         If CharSet1 <> "UTF-8" Then
  13.             .Position = 0
  14.         Else
  15.             .Position = 3 ''无 bom
  16.         End If
  17.         encode = .Read()
  18.         EncodeURI = ""
  19.         For i = 0 To UBound(encode)
  20.             EncodeURI = EncodeURI & "%" & Hex(encode(i))
  21.         Next
  22.         .Close
  23.      End With
  24. err1:
  25.     Set ADODB_Stream = Nothing
  26. End Function
复制代码



评分

2

查看全部评分

TA的精华主题

TA的得分主题

发表于 2023-6-19 11:56 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-6-19 12:23 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
本帖最后由 perfect131 于 2023-6-19 13:50 编辑

只替换 url 中的中文 ,encodeURIhttp
  1. Function encodeURIhttp(ByVal strText As String) As String     '''只替换 url 中的中文
  2.     Set oDom = CreateObject("htmlfile")
  3.     Set Reg = CreateObject("vbscript.regexp")
  4.     With Reg
  5.         .Pattern = "[一-龥]+"
  6.         .Global = True
  7.     End With
  8.     If Reg.test(strText) Then
  9.         Set MC = Reg.Execute(strText)
  10.         For Each M In MC
  11.             strText = Replace(strText, M.Value, oDom.parentWindow.encodeURIComponent(M.Value))
  12.         Next M
  13.         encodeURIhttp = strText
  14.     Else
  15.         encodeURIhttp = strText
  16.     End If
  17.     Set Reg = Nothing
  18.     Set oDom = Nothing
  19. End Function
复制代码
https://club.excelhome.net/forum.php?mod=viewthread&tid=1665474&page=1&_dsign=76abaea8
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-9-24 13:20 , Processed in 0.033820 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表