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

前端之家收集整理的这篇文章主要介绍了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

猜你在找的XML相关文章