poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jim Talbut <jtal...@spudsoft.co.uk>
Subject Problems adding pictures to an XLSX sheet
Date Tue, 27 Sep 2011 10:14:50 GMT
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: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Mime
View raw message