我需要在 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/