title: How to get pictures with names from an xls file using Apache POI
body: <p>Using <a href="https://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFWorkbook.html#getAllPictures()" rel="nofollow noreferrer"><code>workbook.getAllPictures()</code></a> I can get an array of <a href="https://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFPictureData.html" rel="nofollow noreferrer">picture data</a> but unfortunately it is only the data and those objects have no methods for accessing the name of the picture or any other related information.</p>
  
  <p>There is a <a href="https://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFPicture.html" rel="nofollow noreferrer"><code>HSSFPicture</code> class</a> which would contain all the details of the picture but how to get for example an array of those objects from the xls?</p>
  
  <p>Update:</p>
  
  <p>Found SO question <a href="https://stackoverflow.com/questions/21897041/how-can-i-find-a-cell-which-contain-a-picture-in-apache-poi">How can I find a cell, which contain a picture in apache poi</a> which has a method for looping through all the pictures in the worksheet. That works.</p>
  
  <p>Now that I was able to try the <code>HSSFPicture</code> class I found out that the <code>getFileName()</code> method is returning the file name without the extension. I can use the <code>getPictureData().suggestFileExtension()</code> to get a suggested file extension but I really would need to get the extension the picture had when it was added into the xls file. Would there be a way to get it?</p>
  
  <p>Update 2:</p>
  
  <p>The pictures are added into the xls with a macro. This is the part of macro that is adding the images into the sheet. <code>fname</code> is the full path and <code>imageName</code> is the file name, both are including the extension.</p>
  
  <pre><code>Set img = Sheets("Receipt images").Pictures.Insert(fname)
  img.Left = 10
  img.top = top + 10
  img.Name = imageName
  Set img = Nothing
  </code></pre>
  
  <p>The routine to check if the picture already exists in the Excel file.</p>
  
  <pre><code>For Each img In Sheets("Receipt images").Shapes
      If img.Name = imageName Then
          Set foundImage = img
          Exit For
      End If
  Next
  </code></pre>
  
  <p>This recognizes that "image.jpg" is different from "image.gif", so the <code>img.Name</code> includes the extension.</p>