Hi,

I'm adding three pictures to an XLSX sheet and the result is rather odd - the pictures all look correct in Excel, but when I try to move one of them (in Excel) it moves the wrong picture. i.e. I drag Picture 3 to a new location and Picture 1 appears there at Pictures 3's size.

Is this something obvious that I'm doing wrong?

I can't post all my code, so I hope this is enough to show my (hopefully stupid) error:

    private Drawing getDrawing() {
        if( currentDrawing == null ) {
            currentDrawing = currentSheet.createDrawingPatriarch();
        }
        return currentDrawing;
    }

private void placeImageInCurrentCell( int imageIdx, IImageContent image ) {
        System.err.println("Adding image " + imageIdx);
        Cell oldCell = currentCell;
        int widthNum = 0;
        if( currentCell == null ) {
            currentRow = this.currentSheet.createRow(rowNum);
currentRow.setHeightInPoints( StyleManagerUtils.fontSizeInPoints( image.getHeight().toString() ) );

            // Allow image to span multiple columns
            int widthCm = (int)image.getWidth().convertTo( "cm" );
            int currentWidth = 0;
for( widthNum = 0; currentWidth < ( widthCm * 1200 ); ++widthNum ) {
                currentWidth += currentSheet.getColumnWidth( widthNum );
            }
            System.err.println( "Calculated width num: " + widthNum );

            ++rowNum;
            currentCell = currentRow.createCell( 0 );
            currentCell.setCellType(Cell.CELL_TYPE_BLANK);
        } else {
            styleStack.mergeTop(image, ICellContent.class);
            widthNum = currentCell.getColumnIndex() + 1;
        }

        Drawing drawing = getDrawing();

        ClientAnchor anchor = wb.getCreationHelper().createClientAnchor();
        anchor.setCol1(currentCell.getColumnIndex());
        anchor.setRow1(currentCell.getRowIndex());
        anchor.setCol2(widthNum);
        anchor.setRow2(currentCell.getRowIndex() + 1);
        Picture pict = drawing.createPicture(anchor, imageIdx);

        //auto-size picture relative to its top-left corner
        // pict.resize();

        if( oldCell == null ) {
            CellStyle cellStyle = sm.getStyle(image);
            currentCell.setCellStyle(cellStyle);

            currentCell = null;
            currentRow = null;
        }
    }

And then, in another function:

                int imageIdx = wb.addPicture( data, imageType );
                placeImageInCurrentCell( imageIdx, image );

Thanks.

Jim

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

Reply via email to