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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VBA是Visual Basic for Applications的缩写,是一种应用程序语言,主要用于在Microsoft Office软件中编写宏。如果您想将图片转换base64字符串,可以通过以下步骤实现: 1. 打开您想要将图片转换base64字符串的Excel文档。 2. 按下“Alt”和“F11”键,打开Visual Basic编辑器。 3. 在编辑器中选择“插入”>“模块”,创建一个新的VBA模块。 4. 在新模块中输入以下代码: ``` Function Base64EncodeImage(ImagePath As String) As String Dim fs As Object Set fs = CreateObject("Scripting.FileSystemObject") Dim imageFile As Object Set imageFile = fs.GetFile(ImagePath) Dim imageData() As Byte ReDim imageData(imageFile.Size - 1) Dim fileStream As Object Set fileStream = imageFile.OpenAsTextStream(1, False) fileStream.Read imageData Base64EncodeImage = "data:image/png;base64," + Base64Encode(imageData) End Function Function Base64Encode(ByVal inData As Byte) As String 'Convert a byte array to base64 string Const Base64 As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" Dim outData() As Byte ReDim outData((UBound(inData) \ 3) * 4 + 3) Dim byteCount As Long byteCount = 0 Dim i As Long For i = LBound(inData) To UBound(inData) Step 3 outData(byteCount) = Asc(Mid(Base64, (inData(i) \ 4) + 1, 1)) outData(byteCount + 1) = Asc(Mid(Base64, ((inData(i) And 3) * 16) + (inData(i + 1) \ 16) + 1, 1)) If i + 1 <= UBound(inData) Then outData(byteCount + 2) = Asc(Mid(Base64, ((inData(i + 1) And 15) * 4) + (inData(i + 2) \ 64) + 1, 1)) Else outData(byteCount + 2) = Asc("=") End If If i + 2 <= UBound(inData) Then outData(byteCount + 3) = Asc(Mid(Base64, (inData(i + 2) And 63) + 1, 1)) Else outData(byteCount + 3) = Asc("=") End If byteCount = byteCount + 4 Next i Base64Encode = Replace(StrConv(outData, vbUnicode), Chr(0), "") End Function ``` 5. 点击“文件”>“保存”,将模块保存为一个新的文件。 6. 在Excel文档中插入一个图片。 7. 在Excel文档中添加一个新的单元格。 8. 在新单元格中输入以下公式: ``` =Base64EncodeImage("图片路径") ``` 其中,“图片路径”是您想要转换图片的完整路径。例如,如果您的图片在C:\Images\example.png,那么公式应该是: ``` =Base64EncodeImage("C:\Images\example.png") ``` 9. 按下“Enter”键,在新单元格中显示转换后的base64字符串。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值