Excel – 从工作表中的链接图像中提取路径信息



All,我有一个Excel电子表格,其中包含一行图像,这些图像是通过"从文件插入\图片"弹出窗口添加到工作表中的。我没有嵌入图像,而是选择链接到文件的选项。我现在正在将工作表移动到 Access DB,但我无法弄清楚如何从图像行中提取每个链接图像的路径信息?

有谁知道我将如何做到这一点?任何帮助将不胜感激,提前感谢 - CES

通过InsertPicture From File popup window在 Excel 中插入的图片不会另存为路径。它们"活"在 Excel 应用程序中。

为了将它们"移动"到访问数据库,您必须将它们保存在某个地方,从而记住路径。根据这里的答案 导出图片Excel VBA,这是一种方法:

  • 将图片放入图表
  • 固定大小
  • 导出图表
  • 删除图表

Public Sub TestMe()
    Dim pic As Shape
    Dim chrt As Chart
    Dim cnt As Long
    Dim ws As Worksheet
    Dim myPath As String
    Application.ScreenUpdating = False
    myPath = "C:path"
    Set ws = Worksheets(1)
    For cnt = 1 To ws.Shapes.Count
        If InStr(ws.Shapes(cnt).Name, "") > 0 Then
            Set chrt = Charts.Add.Location(Where:=xlLocationAsObject, Name:=ws.Name)
            chrt.ChartArea.Width = ws.Shapes(cnt).Width
            chrt.ChartArea.Height = ws.Shapes(cnt).Height
            chrt.Parent.Border.LineStyle = 0
            ws.Shapes(cnt).Copy
            chrt.ChartArea.Select
            chrt.Paste
            chrt.Export Filename:=myPath & ws.Shapes(cnt).Name & ".jpg"
            ws.ChartObjects(ws.ChartObjects.Count).Delete
        End If
    Next cnt
    Application.ScreenUpdating = True
End Sub

这不是 vba 代码答案,但它可以帮助您实现所需的目标。

如果您的文件以 excel 压缩格式保存,您也许能够获取链接位置,我认为这只是从 excel 2010 开始的:

保存文件的副本并将文件重命名为文件名.zip

解压缩压缩文件.导航文件夹结构:\xl\drawings_rels\

在那里你应该找到参考文件,例如'drawing1.xml.rels'

<?xml version="1.0" encoding="UTF-8" standalone="true"?>
-<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship TargetMode="External" Target="file:///C:UsersexampleuserPicturesmyimage.png" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Id="rId1"/>

从这里您可以提取带有其ID的链接列表

导航到文件夹:\xl\drawings\ 并打开 drawings xml 文件,从这里您可以找到图像 ID 及其 Excel 名称,您还可以从其余的 xml 代码中找出图像所在的行。

-<xdr:pic> 
-<xdr:nvPicPr>
<xdr:cNvPr name="Picture 3" id="4"/>
-<xdr:cNvPicPr>
<a:picLocks noChangeAspect="1"/>
</xdr:cNvPicPr>
</xdr:nvPicPr>
-<xdr:blipFill>
<a:blip r:link="rId1" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"/>
-<a:stretch>
<a:fillRect/>
</a:stretch>
</xdr:blipFill> 
</Relationships>

有了这些信息,您应该能够确定哪些图片链接到哪个文件及其位置。

最新更新