微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

xml – 在Excel VBA中将图像(jpg)转换为base64?

我需要将Excel(或通过VBA)中的图像转换为base64(最后我将生成 XML输出).

我怎样才能做到这一点?我是否需要引用DOM?

我一直在阅读this question,但它只适用于文字字符串而非图像……

有没有人有我能看到的代码

解决方法

这是一个功能.不记得我从哪里得到它.

Public Function EncodeFile(strPicPath As String) As String
    Const adTypeBinary = 1          ' Binary file is encoded

    ' Variables for encoding
    Dim objXML
    Dim objDocElem

    ' Variable for reading binary picture
    Dim objStream

    ' Open data stream from picture
    Set objStream = CreateObject("ADODB.Stream")
    objStream.Type = adTypeBinary
    objStream.Open
    objStream.LoadFromFile (strPicPath)

    ' Create XML Document object and root node
    ' that will contain the data
    Set objXML = CreateObject("MSXml2.DOMDocument")
    Set objDocElem = objXML.createElement("Base64Data")
    objDocElem.dataType = "bin.base64"

    ' Set binary value
    objDocElem.nodeTypedValue = objStream.Read()

    ' Get base64 value
    EncodeFile = objDocElem.Text

    ' Clean all
    Set objXML = nothing
    Set objDocElem = nothing
    Set objStream = nothing

End Function

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。