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

标签 xml excel vba 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 - 在 Excel VBA 中将图像 (jpg) 转换为 base64?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2043393/

相关文章:

java - 我在 Android Studio 中遇到 "inflate exception"错误

android - 您必须提供 layout_height 属性

Excel 向下拖动

regex - Excel公式中的正则表达式

VBA Excel 文件未编译 : Sub or Function not defined

java - 如何加载 N-Triples 模型并从 RDF 文件中获取 namespace ?(新手)

java - 滚动时如何修复折叠工具栏中的 View ?

c# - 使用 excel 文件夹作为我的 Windows 窗体的数据库,我无法访问数据

python - 将 Excel VBA 脚本转换为 Python

excel - VBA 测试单元格是否在范围内