Select Page

偶而會需要將手上蒐集到的會員資料轉成會員資料的SQL語法,其中密碼通常是需要加密再寫入的,因為EXCEL沒有內建加解密服務,當然可以去買擴充套件,但身為RD,要自己來開發一下,可以打開EXCEL中的開發人員模式,並且將下列程式碼,複製貼上後,就可以直接在欄位中把函式當作公式來使用。

Function StringToMD5Hex(ByVal s As String) As String
Dim enc As Object
Dim bytes() As Byte
Dim pos As Long
Dim outstr As String

Set enc = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")

bytes = StrConv(s, vbFromUnicode)
bytes = enc.ComputeHash_2(bytes)

For pos = LBound(bytes) To UBound(bytes)
   outstr = outstr & LCase(Right("0" & Hex(bytes(pos)), 2))
Next pos

StringToMD5Hex = outstr
Set enc = Nothing
End Function

在欄位中當作公式直接使用

=StringToMD5Hex("string to hash")
=StringToMD5Hex(A2)

程式碼要放在開發人員的Visual Basic中

並且檔案格式要是 *.xlsm ,有啟用巨集功能的活頁簿

開啟 Visual Basic 編輯器後,要選取 Module1 也就是寫巨集的地方,然後把程式碼複製上去就可以用了