base64编码 vba_转换图像(JPG)为base64在Excel VBA?

本文介绍如何在Excel VBA中将图像转换为Base64编码。通过创建ADODB.Stream对象读取二进制图片,然后使用MSXML2.DOMDocument对象将图片数据转换为Base64字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值