EXCEL VBA计算带汉字的字符串的MD5值(VBA转UTF-8保存)
作者:互联网
操作思路:
1.使用 ADODB.stream 把字符串转换为UFT-8的编码集存储到文件 AA
2.使用调用windows命令行CertUtil -hashfile c:\11\AA md5 对文件计算MD5值
3.回显结果直接输出到EXCEL
4.字符串截取第二行,去空格,输出MD5值
VBA代码:
strBody = "VBA变量字符串转换为UFT-8后MD5加密"
'MD5计算
Dim stream As New ADODB.stream
stream.Open
stream.Type = adTypeText
stream.Charset = "utf-8"
stream.WriteText strBody
' Try to Remove UTF-8 BOM
stream.Position = 3 'skip BOM
Dim BinaryStream As New ADODB.stream
BinaryStream.Type = adTypeBinary
BinaryStream.Mode = adModeReadWrite
BinaryStream.Open
' Strips BOM (first 3 bytes)
stream.CopyTo BinaryStream
stream.Flush
stream.Close
str8 = BinaryStream.Read
BinaryStream.SaveToFile "C:\11\AA", adSaveCreateOverWrite
BinaryStream.Flush
BinaryStream.Close
Dim WSH, wExec, sCmd As String
Set WSH = CreateObject("WScript.Shell")
Set wExec = WSH.Exec("CertUtil -hashfile c:\11\AA md5")
Ar = Split(wExec.StdOut.ReadAll, vbCrLf)
N = UBound(Ar)
Sign = Replace(Ar(1), Space(1), "")
Set wExec = Nothing
Set WSH = Nothing
Range("A12") = strBody
标签:wExec,VBA,UTF,stream,WSH,EXCEL,ADODB,MD5 来源: https://blog.51cto.com/mflag/2473469