在Excel VBA中将图像(jpg)转换为base64? [英] Convert image (jpg) to base64 in Excel VBA?

查看:65
本文介绍了在Excel VBA中将图像(jpg)转换为base64?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to convert an image inside Excel (or through VBA) to base64 (in the end I will make XML output).

我该怎么做?我需要引用 DOM 吗?

How can I do this? Do I need to make a reference to DOM?

我一直在阅读 这个问题 但它只适用于文本字符串而不是图像......

I´ve been reading this question but it only works for text strings not images...

谁有我能看到的代码?

推荐答案

这里有一个函数.不记得我从哪里得到的.

Heres a function. Can't remember where I got it from.

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

这篇关于在Excel VBA中将图像(jpg)转换为base64?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆