Hi,

I'm just creating a piece of code that exports some data - including one image 
per row - to an excel xlsx file with Apache POI. I'm using the 
AddDimensionedImage example class and it works well. But if I open the 
resulting file with excel and move one of the images for the first time, the 
first image will be moved instead. If I then move another image, the last image 
will be moved instead, but not the image I like to move.
If I save the file with excel before I move any image the above described 
behavior does not arise.

I wrote a little bit of code to reproduce the behavior (all you need in 
addition are three images A.png, B.png and C.png):

public class ImageExportTest {

  private final URL[] pictureURL = new URL[3];

  public ImageExportTest() throws MalformedURLException {
    this.pictureURL[0] = new URL("file:///tmp/A.png");
    this.pictureURL[1] = new URL("file:///tmp/B.png");
    this.pictureURL[2] = new URL("file:///tmp/C.png");
  }

  public static void main(final String[] args) {
    try {
      final ImageExportTest exportTest = new ImageExportTest();
      exportTest.export();
    } catch (final Exception e) {
      e.printStackTrace();
    }
  }

  private void export() throws IOException {
    final Workbook workbook = new XSSFWorkbook();
    final Sheet sheet = workbook.createSheet("ImageExportTest");
    for (int rowNo = 0; rowNo < 3; rowNo++) {
      final Row row = sheet.createRow(rowNo);
      row.setHeightInPoints(75);
      // image cell
      Cell cell = row.createCell(1);
      cell.setCellValue("");
      cell = row.createCell(2);
      cell.setCellValue("image " + ("ABC".charAt(rowNo)));
    }
    sheet.setColumnWidth(1, 5000);
    final Drawing drawing = sheet.createDrawingPatriarch();
    final CreationHelper helper = workbook.getCreationHelper();
    for (int rowNo = 0; rowNo < 3; rowNo++) {
      final InputStream is = this.pictureURL[rowNo].openStream();
      final byte[] bytes = IOUtils.toByteArray(is);
      final int index = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
      is.close();
      final ClientAnchor anchor = helper.createClientAnchor();
      anchor.setCol1(1);
      anchor.setRow1(rowNo);
      final Picture pict = drawing.createPicture(anchor, index);
      pict.resize();
    }
    final FileOutputStream fileOut = new 
FileOutputStream("/tmp/imageExportTest.xlsx");
    workbook.write(fileOut);
    fileOut.close();
  }
}

Maybe anyone has any idea how to solve this issue?

Regards and thanx in advance,
Torsten

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to