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

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

问题描述

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



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



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



有没有人可以看到任何代码?

解决方案

不记得我从哪里获取了。

 公共功能EncodeFile(strPicPath As String)As String 
Const adTypeBinary = 1'二进制文件编码

'编码变量
Dim objXML
Dim objDocElem

'读取二进制图片的变量
Dim objStream

'从图片打开数据流
设置objStream = CreateObject(ADODB.Stream)
objStream.Type = adTypeBinary
objStream.Open
objStream.LoadFromFile(strPicPath)

'创建XML文档对象和根节点
'将包含数据
设置objXML = CreateObject(MSXml2.DOMDocument)
设置objDocElem = objXML.createElement(Base64Data)
objDocElem.dataType =bin.base64

'设置二进制值
objDocElem.nodeTypedValue = objStream.Read()

'获取base64值
EncodeFile = objDocElem.Text

'清除所有
设置objXML =没有
设置objDocElem =没有
设置objStream =没有

结束函数


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

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...

Does anyone have any code that I can see?

解决方案

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

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

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